I have a data file containing 100 lines with the following format
0,device1,3 1,device2,33 2,device3,3 3,device4,34 ... 99,device100,36
Now I wish to read them into a 100x3
cell array in MATLAB. I did the following:
allData = textscan(fID,'%s %s %f', 'delimiter', ',');
Then, I noticed that allData
is a 1x3
cell array with each item being another 100x1
cell array. (The first two columns are string-type cell arrays, whereas the third column is double-type cell array)
In other words, the reading result is a nested
array, which I don't want.
How may I achieve 100x3
cell array directly while reading?
Code -
Thus, the first four lines would be shown as -
With that
textscan
, the variableallData
looks something like (just 4 rows) this:You can only merge into a single cell array directly with
textscan
via the'CollectOutput'
option when all data has the same type.One possible workaround, which unfortunately converts all numeric data to double (not a problem in your case),
Again, the drawback of this is that the last statement will convert all non-cell types (e.g. uint8, char, etc.) into doubles. To avoid this possible conversion: