How Read text file as array form , Please

Hi ,
i have text file (data) , i need store each word or number as array (x,y):as in attach file
for example:
line 1: 413733000: system.cpu15.icache: ReadReq [10574:10577] IF hit state: 5 (S) valid: 1 writable: 0 readable: 1 dirty: 0 tag: 8
line2: 413733000: system.cpu00.icache: ReadReq [6e4ef8:6e4efb] IF hit state: 5 (S) valid: 1 writable: 0 readable: 1 dirty: 0 tag: 372
store in array as :
c(1,1)=413733000 c(1,2)= system c(1,3)=cpu15 c(1,3)= icache ...etc. to the line 1
c(2,1) =413733000 c(2,1)=system c(2,2)=cpu00 c(2,3)=ichahe ..etc.. to the line 2
i appricate for any help

4 comentarios

Rik
Rik el 21 de Ag. de 2019
What have you tried so far?
Furat Alobaidy
Furat Alobaidy el 21 de Ag. de 2019
i need convert this text into table or matrix (coulms and rows ) for more processing.
for example :i need arrange as table for each line :
CPU name (00) : Memory address [ 0000 ] , readable : (1 or 0) , writable :(0 or 1) , dirty :(0 or 1) , hit(S or M)
Rik
Rik el 21 de Ag. de 2019
You misunderstood: I meant what code did you already try.
Also, it might be a better idea to parse each line, instead of chopping it up into smaller pieces. And you probably want a struct array.
Furat Alobaidy
Furat Alobaidy el 21 de Ag. de 2019
filename = 'C:\xxx\sample2.txt';
fileID = fopen(filename);
%c = textscan(fileID,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s[^\n]' );
fclose(fileID);
but its gave me just one vector c(1) {19) for all lines !
i need matrix : rows = lines (1..2)
colums = (1..19)
"Also, it might be a better idea to parse each line, instead of chopping it up into smaller pieces. And you probably want a struct array."
can provide me more hints please

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 21 de Ag. de 2019
The code below should get you most of the way there. The idea is to parse each element and then remove it from the variable, so every part is independent and can in principle be replaced by some parsing function you would write. This code does rely on you having read the file to a cell array, but that shouldn't really be a problem. You can even use my readfile FEX function if you're in a hurry.
c={'413733000: system.cpu15.icache: ReadReq [10574:10577] IF hit state: 5 (S) valid: 1 writable: 0 readable: 1 dirty: 0 tag: 8',...
'413733000: system.cpu00.icache: ReadReq [6e4ef8:6e4efb] IF hit state: 5 (S) valid: 1 writable: 0 readable: 1 dirty: 0 tag: 372'};
out=struct;
for n=1:numel(c)
str=c{n};
idx=strfind(str,':');
ID=str(1:(idx(1)-1));
str(1:(idx(1)+1))='';%chop off ID
idx=strfind(str,':');
CPU=str(1:(idx(1)-1));
str(1:(idx(1)+1))='';%chop off CPU
idx1=strfind(str,'[');idx1=idx1(1)+1;
idx2=strfind(str,']');idx2(idx2<idx1)=[];idx2=idx2(1)-1;
MemoryAddress=str(idx1:idx2);
str(1:(idx2+2))='';%chop off MemoryAddress
%etc
%store to output struct
out(n).ID=ID;
out(n).CPU=CPU;
out(n).MemoryAddress=MemoryAddress;
%etc
end

4 comentarios

Furat Alobaidy
Furat Alobaidy el 21 de Ag. de 2019
Thanks a lot for a great help, really i appreciate for your helping.best regards.
Furat Alobaidy
Furat Alobaidy el 21 de Ag. de 2019
i apologize for that , if i want read fromthe text file directly , it gave me error :
Cell contents reference from a non-cell array object.
Error in p1(line 10)
str=c{n};
i added this commands : to input the data from the text file !
filename = 'C:\Users\xxx\sample1.txt';
c = fopen(filename,'r');
out=struct;
for n=1:numel(c)
str=c{n};
idx=strfind(str,':');
ID=str(1:(idx(1)-1));
str(1:(idx(1)+1))='';%chop off ID
idx=strfind(str,':');
CPU=str(1:(idx(1)-1));
str(1:(idx(1)+1))='';%chop off CPU
idx1=strfind(str,'[');idx1=idx1(1)+1;
idx2=strfind(str,']');idx2(idx2<idx1)=[];idx2=idx2(1)-1;
MemoryAddress=str(idx1:idx2);
str(1:(idx2+2))='';%chop off MemoryAddress
%etc
%store to output struct
out(n).ID=ID;
out(n).CPU=CPU;
out(n).MemoryAddress=MemoryAddress;
%etc
end
fclose(fileID);
Rik
Rik el 21 de Ag. de 2019
Have you read the documentation for fopen? It generates a handle to a file, it isn't the file contents. If you have an error or unexpected output when using a function, read the documentation first.
Either use fgetl (and replace the for-loop by a while loop, don't forget setting n to 0 and incrementing it inside the loop) or use my readfile function. Note that it is often much faster to load the entire file that to read line by line.
Furat Alobaidy
Furat Alobaidy el 21 de Ag. de 2019
thanks a lots ,

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 21 de Ag. de 2019

Comentada:

el 21 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by