Clear Filters
Clear Filters

Can someone help me to explain/understand this code

1 view (last 30 days)
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));

Accepted Answer

Jan
Jan on 26 May 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 Comments
Nisar Ahmed
Nisar Ahmed on 26 May 2021
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 on 26 May 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!