Question

In MATLAB, I have a string with the following format,

str = 'var1,var2\nvar1,var2,var3\n,var9\n'\n\n,var12,var13,var14,v15;

I want to convert this to a cell matrix where each comma , separate's a different item in a column and each \n represents a new row. Each value can either be a string or double.

There can be empty rows, and many empty values. The largest row can be any row.

I know I can loop split the string and loop through it adding to the cell matrix as I go along, but is there a more efficient way? How can I efficiently convert the above string to a cell matrix.

Was it helpful?

Solution

You can make use of strsplit and/or regexp to get the row,col elements in individual cells:

>> strsplit(str,'\\n')
ans = 
    'var1,var2'    'var1,var2,var3'    ',var9'    ',var12,var13,var14,v15'
>> tab = regexp(strsplit(str,'\\n'),',','split');
>> tab{:}
ans = 
    'var1'    'var2'
ans = 
    'var1'    'var2'    'var3'
ans = 
    ''    'var9'
ans = 
    ''    'var12'    'var13'    'var14'    'v15'

Then you can access the cells like:

>> tab{2}{3}
ans =
var3

This is not a true 2D cell, but it may serve the purpose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top