Skipping through text file and extracting data
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello
I have a dump output from another program. A short example of which is below
ITEM: TIMESTEP
1000
ITEM: BOUNDS
0 10
0 10
0 10
ITEM
id type xs ys zs
1 1 0.1 0.1 0.2
3 1 0.5 0.5 0.4
2 1 0.2 0.3 0.4
it then repeats within the same file for other timesteps. I want it to search for a specific timestep within the file (say 1000), then skip down to 'id type xs ys zs' and save the values xs,ys,zs into a (n,3) array. It would need to stop reading when it detects that the line is no longer numeric.
To me this seems like a simple problem, but it appears that matlaab can only read in full files, as I cannot find a 'skip x lines' function, or 'find value in text file' option. Does matlab keep this behind the scenes and not have it as an option for users?
Jay
0 Commenti
Risposte (1)
Chirag Gupta
il 15 Giu 2011
There is no direct solution for this, but you code it up quite easily:
% num is a vector of interested timesteps
function data = readdump(filename,num)
fid = fopen(filename,'r');
% This will be in for loop:
% for i=1:length(num)
tline = fgetl(fid);
% call to check num
if(checknum(fid,num,tline)) % reality pass num(i)
% call function to read data
% you can use the file structure knowledge to your advantage
% use textscan to skip lines and read data in the desired format
disp('Read Data');
else
% skip lines to the next ITEM (again , you can use textscan (again if you know the struture of the file and its consistent, then its simpler
disp('Skipping');
end
data = 0;
function out = checknum(fid,num,tline)
if(strcmp(tline,'ITEM: TIMESTEP'))
% read next line
c = textscan(fid,'%d',1);
if(c{1} == num)
out = true;
else
out = false;
end
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!