按字符数matlab分割字符串

时间:2013-03-28 20:48:44

标签: string matlab octave

Matlab中是否有任何内置函数可以按字符数切割字符串并将其作为单元格数组返回。例如,如果调用A = some_function(string,3):

Input: string = '1234567890'
Output: A = {'123', '456', '789', '0'}

还是我需要使用循环?

感谢。

2 个答案:

答案 0 :(得分:6)

另一种解决方案,稍微优雅(在我看来),将使用regexp

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

其中str是您的字符串,n是字符数。

实施例

>> regexp('1234567890', '\w{1,3}', 'match')

ans = 
    '123'    '456'    '789'    '0' 

答案 1 :(得分:3)

有点长可能是:

ns = numel(string);
n = 3;
A = cellstr(reshape([string repmat(' ',1,ceil(ns/n)*n-ns)],n,[])')'
相关问题