MATLAB: How to split strings equally with a rest

split string equally

i want to split a string in equal parts like in this example:input='abcdefghijk' output='abc def ghi jk'

Best Answer

input_string ='abcdefghijk' % don’t use variable name as input because it may contradict with built-in function
ns = numel(input_string);
n = 3;
Output = cellstr(reshape([input_string repmat(' ',1,ceil(ns/n)*n-ns)],n,[])')' % as cell
Output = char(Output) % as char