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

7 Ansichten (letzte 30 Tage)
Akana Juliet
Akana Juliet am 16 Jun. 2021
Kommentiert: Akana Juliet am 17 Jun. 2021
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)';

Akzeptierte Antwort

Stephen23
Stephen23 am 16 Jun. 2021
Bearbeitet: Stephen23 am 16 Jun. 2021
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 Kommentare
Stephen23
Stephen23 am 17 Jun. 2021
"Is there anyway to format the Data file to be in 3 2x8 "subcells..."
No, that is not possible: your data file only has 24 hex values, so your request for 3*2*8 = 48 hex values does not reflect what the file actually contains. It is possible to arrange them into three groups of 2x4, just like the file:
out = mat2cell(reshape(tkn(:,2),4,[]).',[2,2,2],4)
out = 3×1 cell array
{2×4 cell} {2×4 cell} {2×4 cell}
out{:}
ans = 2×4 cell array
{'4e949'} {'53c55'} {'3433c'} {'f55f6'} {'ca5be'} {'3d028'} {'caaf5'} {'3433c'}
ans = 2×4 cell array
{'2bec81'} {'16e9b3'} {'002a53'} {'125da7'} {'c87c63'} {'b0a193'} {'491d99'} {'2bec81'}
ans = 2×4 cell array
{'951c6bb'} {'bb0b954'} {'e2a366d'} {'da12937'} {'772e186'} {'f40d336'} {'67187fb'} {'df63118'}
Akana Juliet
Akana Juliet am 17 Jun. 2021
Thank you! I deleted my other question because this is working so well, thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by