Problems getting the size of a cell array

25 Ansichten (letzte 30 Tage)
Anshuman Pal
Anshuman Pal am 14 Apr. 2020
Kommentiert: Anshuman Pal am 14 Apr. 2020
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?
  1 Kommentar
Anshuman Pal
Anshuman Pal am 14 Apr. 2020
Sorry, the .rpt above and in the code should be substitued with .txt.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 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);
  1 Kommentar
Anshuman Pal
Anshuman Pal am 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by