Problems getting the size of a cell array
21 views (last 30 days)
Show older comments
Hi,
I know that one can usually get the size of a cell array using size(). However, I am facing this particular case -- where I am trying to read data from a .txt file, and save it to a matrix -- where a particular row of the cell (data.textdata{2}) just refuses to show its actual size on using size(). I need the length to loop over data.textdata{2}.
filename = 'test'
data = importdata([filename '.rpt']) ;
%% data is in data.textdata. In particular, data.textdata{2}, which is a matrix, refuses to show its size.
size(data.textdata{2}) % only gives the size of the first character array
I am attaching the code and the text file. Can someone please help?
Answers (1)
Walter Roberson
on 14 Apr 2020
You have
data.textdata{2,1} = [];
That does not delete an existing data.textdata{2,1} : it assigns the empty array inside it.
size(data.textdata{2})
{2} is linear indexing into the 7 x 17478 cell array, and is equivalent to {2,1} when there is more than one row. So size(data.textdata{2}) is the same as size(data.textdata{2,1}) which is the object you just set to [], so the returned size is, quite correctly, 0 x 0.
If you want to know how many entries are in the row, then size(data.textdata,2) --- where 2 refers to the second dimension, not to row number 2.
I would suggest to you that you instead use
data = table2array(readtable([filename '.rpt'], 'headerlines', 4));
and if you have r2019b or later,
data = readmatrix([filename '.rpt'], 'headerlines', 4);
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!