Problems getting the size of a cell array

24 ビュー (過去 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 Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by