How can I split a cell array in which every cell has strings and numbers but the space made them split?

 Accepted Answer

You can use strsplit() or you can use regexp() with the 'split' option.

4 Comments

It doesn't work. It is cell arrays. Cell one =
RESERV 1 1928 552.45 551.82 551.70 552.47 553.37 556.57 556.86 556.30 556.40 556.21 555.72 554.10
cell_one = {'RESERV 1 1928 552.45 551.82 551.70 552.47 553.37 556.57 556.86 556.30 556.40 556.21 555.72 554.10'};
result = regexp(cell_one, '\s+', 'split');
result =
cell
{1×15 cell}
>> result{1}
ans =
1×15 cell array
Columns 1 through 14
'RESERV' '1' '1928' '552.45' '551.82' '551.70' '552.47' '553.37' '556.57' '556.86' '556.30' '556.40' '556.21' '555.72'
Column 15
'554.10'
Now, they are all characters. But we know that the first one is a string and the others are numbers. How can I send them into a matrix that because I want to plot RESERV 1 in 1928 based on the other numbers.
result_num = cell2mat(cellfun(@(C) str2double(C(2:end)), result, 'Uniform', 0));

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!