How to copy cell data to a matrix?

2 ビュー (過去 30 日間)
Navid Mohammad Imran
Navid Mohammad Imran 2020 年 8 月 11 日
コメント済み: hosein Javan 2020 年 8 月 11 日
I have a text file where I use to read comma-delimited data into a cell array. The text file contains multiple lines of data so now I want to run a loop to copy some of the data read to the 2-D cell array into a matrix. This is a snippet of my code:
A = zeros(4,6);
%Read data
while ischar(c)
c = textscan(fileID,'%s %s %s %s',6,'Delimiter',',');
end
for j = 1:6
for i = 1:4
if mod(j,2) == 0
A(i,j) = c{3}{i};
else
A(i,j) = c{4}{i};
end
end
end
I keep getting this error:
the size of the left side is 1-by-1 and the size of the right side is 1-by-8
This is a sample of the text file I am reading with textscan:
10,2008-02-02 13:32:03,116.44457,39.92157
10,2008-02-02 13:33:58,116.44043,39.9219
Can you tell me how to fix this? I have tried using but it gives me the following message:
cell2mat is not supported for cell arrays containing cell arrays or objects.

採用された回答

hosein Javan
hosein Javan 2020 年 8 月 11 日
編集済み: hosein Javan 2020 年 8 月 11 日
the problem is with the date/time format which contains ":" and "-'. if you need matrix you can simply avoid these.
c='';
fileID = fopen('scan1.txt'); % file that contains formatted data
while ischar(c)
c = textscan(fileID,'%f,%f-%f-%f %f:%f:%f,%f,%f');
end
c = cell2mat(c)
ans =
10 2008 2 2 13 32 3 116.44 39.922
10 2008 2 2 13 33 58 116.44 39.922
  2 件のコメント
Navid Mohammad Imran
Navid Mohammad Imran 2020 年 8 月 11 日
Thank you, this works for me.
hosein Javan
hosein Javan 2020 年 8 月 11 日
you're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by