I am creating a .dat file with headers and data in Matlab like this:
%# Define the name of the file where data is to be written
dataName = 'sampledata_models_poro_reference_truth';
%# open the file
fid = fopen('sampledata_models_poro_reference_truth.dat','w');
%# start writing. First line: title
fprintf(fid,'%s\n',dataName); %# don't forget \n for newline. Use \n\r if yow want to open this in notepad
%# write number of models
fprintf(fid,'%i\n',nModel)
%# loop to write the rest of the header
for iModel = 1:nModel
fprintf(fid,'%s_%i\n',dataName,iModel);
end
%# use your favorite method to write the rest of the data.
%# for example, you could use fprintf again, using \t to add tabs
%# create format-string
%# check the help to fprintf to learn about formatting details
formatString = repmat('%f\t',1,nModel);
formatString = [formatString(1:end-1),'n']; %# replace last tab with newline
%# transpose the array, because fprintf reshapes the array to a vector and
%# 'fills' the format-strings sequentially until it runs out of data
fprintf(fid,formatString,sampledata_models_poro_reference_truth');
%# close the file
fclose(fid);
The file gets created, however when I try opening it from the folder where it is saved, then I get the error as shown in the snapshot below. This error occurs even when Matlab is closed. I can only open it as a text by right clicking on the file through the Matlab's current directory window.
Why is this happening and how to resolve it?