How to read in more rows to a cell array from a text file?

2 ビュー (過去 30 日間)
Akana Juliet
Akana Juliet 2021 年 6 月 16 日
コメント済み: Akana Juliet 2021 年 6 月 17 日
Hi all, I needed some help with my code. My code is meant to read in a text file (I've attached a shortened version of what the actual looks like) line by line and put only the numbers into a cell array. (I also wanted to add that I can change the String Category titles in the text document but I cannot change the content under titles.
Stored cell in MATLAB currently looks like this:
0 15 6 2 3
1 3 2 4 0
2 1 6 4 7
3 0 1 4 7
I have two major questions here:
  1. How can I make this code produce more than just 4 rows? I either need to scan in all of the index under one cell array or create a cell array for each index category, whichever is easier. When I tried to change the 4 in the reshape line it said: "Error using reshape To RESHAPE the number of elements must not change." I got help on this and it was set as a 1x20 matrix (20 elements) and changed to a 5x4 matrix (20 elements) then transposed. I am not sure if there is a better way of doing this especially for a longer textfile.
  2. What type of tweaks would I need to make to this same code for reading a very similar text file, except with hexadecimal values instead of decimal values? I need the hexidecimal values stored as is, not converted to anything. but the array numbers on the left. I have attached the file "data.txt" to show you what that looks like.
Here is the code so far for the index file:
fid = fopen('index.txt','r');
t = textscan(fid,'%s','delimiter','\t'); %this outputs into just one column
fclose(fid);
% converting strings to numeric types
% changing strings to empty values
t2 = t;
for n = 1:length(t2{1})
t2{1}{n} = str2num(t2{1}{n});
end
% reshapes the 1x20 row matrix into a 5x4 matrix and then transposes it
% Reshape goes down the column first
reshape(t2{1}{2},5,4)';

採用された回答

Stephen
Stephen 2021 年 6 月 16 日
編集済み: Stephen 2021 年 6 月 16 日
This would be easier if each block used an integer instead of "First", "Second", etc. By far the best would be leading text followed by the integer, e.g. "Index 1", "Data 1", etc. The attached files include these changes.
Then you can efficiently import all of the file data, e.g.:
dat = {};
fid = fopen('data.txt','rt');
while ~feof(fid)
fscanf(fid,'Data%d');
dat{end+1} = fscanf(fid,'%d%x',[8,Inf]).';
end
fclose(fid);
dat{:}
ans = 2×8
0 321865 1 343125 2 213820 3 1005046 4 828862 5 249896 6 830197 7 213820
ans = 2×8
0 2878593 1 1501619 2 10835 3 1203623 4 13139043 5 11575699 6 4791705 7 2878593
ans = 2×8
0 156354235 1 196131156 2 237647469 3 228665655 4 124969350 5 255906614 6 108103675 7 234238232
idx = {};
fid = fopen('index.txt','rt');
while ~feof(fid)
fscanf(fid,'Index%d');
idx{end+1} = fscanf(fid,'%d',[20,Inf]).';
end
fclose(fid);
idx{:}
ans = 2×20
0 15 6 2 3 1 3 2 4 0 2 1 6 4 7 3 0 1 4 7 4 0 5 6 1 5 0 1 2 3 6 0 5 6 2 7 0 3 6 7
ans = 2×20
0 0 6 2 3 1 3 2 4 0 2 1 6 4 7 3 0 1 4 7 4 0 5 6 1 5 0 7 2 3 6 0 5 6 2 4 0 5 6 7
ans = 2×20
0 4 6 2 3 1 3 2 4 0 2 1 6 2 7 3 0 1 4 7 4 5 0 6 1 5 0 1 2 3 6 0 5 6 2 7 0 5 6 0
  6 件のコメント
Akana Juliet
Akana Juliet 2021 年 6 月 17 日
Thank you! I deleted my other question because this is working so well, thank you!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by