Can someone help me to explain/understand this code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nisar Ahmed
am 26 Mai 2021
Kommentiert: Nisar Ahmed
am 4 Jun. 2021
if nargin==0, [file,pathname]=uigetfile('*.las'); else pathname=''; end;
if ischar(file)
fid=fopen([pathname,file]);
k=0; header=''; colblock=0; datablock=0;
while ~datablock
jline=fgetl(fid);
if ~ischar(jline), break, end;
header=strvcat(header,jline);
if any(strmatch('~A',upper(jline))), colblock=0; datablock=1; end;
if colblock&any(strmatch('~',upper(jline))), colblock=0; end;
if colblock & ~strcmp(jline(1),'#'),
k=k+1; colnames{k}=strtok(jline);
end;
if any(strmatch('~CURVE',upper(jline))), colblock=1; datablock=0; end;
end;
disp(' ');
disp(['Reading data for ' num2str(k) ' log curves from ' file])
data=fscanf(fid,'%f');
data=reshape(data,[k length(data)/k])';
fclose(fid);
data(data<=-999)=nan;
colnames=regexprep(colnames,'\W','_');
colnames=lower(colnames);
datastr.header=header;
for k=1:length(colnames)
datastr = setfield(datastr,colnames{k},data(:,k));
2 Kommentare
Akzeptierte Antwort
Jan
am 26 Mai 2021
This code imports a text file and parses its contents. It does not contain any comments, therefore it is not useful for productive work. Without a documentation the readers have to guess, what its intention is.
Some commands are strange:
if colblock&any(strmatch('~',upper(jline)))
When searching for the character ~, there is no reason to convert the char vector to uppercase. Simpler and nicer:
if colblock & strncmp(jline, '~', 1)
Or in modern Matlab versions prefer startsWith(jline, '~').
strmatch, setfield, strvcat - this is an old code using deprecated commands.
The best way to understand, what the code does is to open a .las file and step through the code line by line during it is running. Simply set a breakpoint in the first line and use the Step-Button in the debugger.
2 Kommentare
Walter Roberson
am 26 Mai 2021
if colblock&any(strmatch('~',upper(jline)))
That looks through upper(jline) to see if any of the characters in it are the ~ character.
if colblock & strncmp(jline, '~', 1)
That looks at the first character of jline to see if it is the ~ character. Not the same.
if colblock && ismember('~', jline)
would be better.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!