Can someone help me to explain/understand this code

조회 수: 1 (최근 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
Jan
Jan 2021년 6월 1일
Please do not remove the question after answers have been given.
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
Nisar Ahmed
Nisar Ahmed 2021년 5월 26일
Hi Jan,
here are ther comments.
%[DATASTR,DATA,COLNAMES,HEADER]=LOADINGLASFILE(FILE)
% Load LAS (Log Ascii Standard) formatted well log file
%
% FILE (optional): Input filename of file in LAS format.
% FILE should be character array within single quotes 'file'.
% Without input arguments displays dialogbox for file name.
% DATA: the numeric data matrix.
% COLNAMES: names of the columns as specified in file (cell array)
% HEADER: Header of the file as string matrix.
% DATASTR: Structure array containing HEADER and the COLNAMES as its fields.
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개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by