Can someone help me to explain/understand this code

2 ビュー (過去 30 日間)
Nisar Ahmed
Nisar Ahmed 2021 年 5 月 26 日
コメント済み: Nisar Ahmed 2021 年 6 月 4 日
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 件のコメント
Nisar Ahmed
Nisar Ahmed 2021 年 6 月 4 日
Hi Jan, OK

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

採用された回答

Jan
Jan 2021 年 5 月 26 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 26 日
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.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by