Splitting string by number of characters matlab

最后都变了- 提交于 2019-12-01 19:44:03

A little long may be:

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

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' 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!