필터 지우기
필터 지우기

Problems getting the size of a cell array

조회 수: 26 (최근 30일)
Anshuman Pal
Anshuman Pal 2020년 4월 14일
댓글: Anshuman Pal 2020년 4월 14일
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
Anshuman Pal
Anshuman Pal 2020년 4월 14일
Sorry, the .rpt above and in the code should be substitued with .txt.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 4월 14일
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
Anshuman Pal
Anshuman Pal 2020년 4월 14일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by