Problems getting the size of a cell array

24 views (last 30 days)
Anshuman Pal
Anshuman Pal on 14 Apr 2020
Commented: Anshuman Pal on 14 Apr 2020
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?
  1 Comment
Anshuman Pal
Anshuman Pal on 14 Apr 2020
Sorry, the .rpt above and in the code should be substitued with .txt.

Sign in to comment.

Answers (1)

Walter Roberson
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.
{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);
  1 Comment
Anshuman Pal
Anshuman Pal on 14 Apr 2020
Thank you for suggesting readmatrix().
However, I still have a problem -- I want to use sscanf() to scrape the numbers 100, 101, 102, ... from the third header line:
X SHORT-1 N: 100 SHORT-1 N: 101 SHORT-1 N: 102 ...
Can you suggest a way of doing this?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!