This file is an output file whose first line is a header followed by n rows with data. This first set of data is followed by more similar sets of data with different values.
I want to read the 2nd and 3rd column from this file for all sets of data i.e direction 1, direction 2, etc. Currently I am using the following lines of code inside a function to read the data as shown below:
fid = fopen(output_file); % open the output file
dotOUT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotOUT_fileContents = dotOUT_fileContents{1};
fclose(fid); %# close the file
%# find rows containing 'SV'
data_starts = strmatch('SV',...
dotOUT_fileContents); % data_starts contains the line numbers wherever 'str2match' is found
nDataRows=data_starts(2)-data_starts(1)-1;
ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of 'str2match' read from the .out file
%# loop through the file and read the numeric data
for w = 1:ndata
%# read lines containing numbers
tmp_str = dotOUT_fileContents(data_starts(w)+1:data_starts(w)+nDataRows);
%# convert strings to numbers
y = cell2mat(cellfun(@(z) sscanf(z,'%f'),tmp_str,'UniformOutput',false)); % store the content of the string which contains data in form of a character
data_matrix_column_wise(:,w) = y; % convert the part of the character containing data into number
%# assign output in terms of lag and variogram values
lag_column_wise(:,w)=data_matrix_column_wise(2:6:nLag*6-4,w);
vgs_column_wise(:,w)=data_matrix_column_wise(3:6:nLag*6-3,w);
end
This function was working good if I didn’t have the stars shown in the output file above. However, one of the output file like the one shown above contained stars and the above code failed in that case. What should be done to take care of the stars inside the data such that I am able to correctly read columns 2 and 3?
The problem is this part of your code:
You’re forcing a match to a floating point number, and when it encounters stars it fails. You may be better off replacing that with something like
and remove the cell2mat and modify the following lines appropriately to test for the presence of strings.
Alternatively, and this depends on what those stars mean, you could just replace the stars with a zero or something that makes sense, and your current code would work fine.