Splitting string by number of characters matlab

后端 未结 2 622
慢半拍i
慢半拍i 2021-01-19 19:05

Is there any builtin function in Matlab that cuts string by the number of characters and returns it as a cell array or something. For example if call A = some_function(strin

相关标签:
2条回答
  • 2021-01-19 19:34

    A little long may be:

    ns = numel(string);
    n = 3;
    A = cellstr(reshape([string repmat(' ',1,ceil(ns/n)*n-ns)],n,[])')'
    
    0 讨论(0)
  • 2021-01-19 19:55

    An alternative solution, which is slightly more elegant (in my opinion), would be using regexp:

    A = regexp(str, sprintf('\\w{1,%d}', n), 'match')
    

    where str is your string and n is the number of characters.

    Example

    >> regexp('1234567890', '\w{1,3}', 'match')
    
    ans = 
        '123'    '456'    '789'    '0' 
    
    0 讨论(0)
提交回复
热议问题