Please tell me what is the outfile in this code

5 views (last 30 days)
[M,N,P,color] = size(phi);
disp('Writing to a file ...')
outfile = ['C:\temp\explicit_ball\output' int2str(n) '.img'];
fwriteid = fopen('outfile','w');
count = fwrite(fwriteid,phi,'float');
status = fclose(fwriteid);
I am not able to understand the following steps of this code
outfile = ['C:\temp\explicit_ball\output' int2str(n) '.img'];
fwriteid = fopen('outfile','w');
count = fwrite(fwriteid,phi,'float');
Please help me as to what is the extension of the outfile and where is it being stored?
Thanks a lot in advance

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2014
The first line creates the variable outfile as a string. The string will be of the form
C:\temp\explicit_ball\output#.img
where # is replaced by a number.
The second line, fwriteid = fopen('outfile','w'); ignores the string that was just created in the variable outfile, and instead passes in the literal string 'outfile'. An fopen() for a file named 'outfile' is not going to attach any extension at all.
What the line should have been is
fwriteid = fopen(outfile,'w');
and then the value of the variable "outfile" would be used, thus opening
C:\temp\explicit_ball\output#.img
where # is replaced by an integer.
The third line,
count = fwrite(fwriteid,phi,'float');
writes the variable "phi" to the current file, writing it out in binary single precision format ('float'). The number of elements written (not the number of bytes) is stored into "count".
  2 Comments
Nalini
Nalini on 17 Feb 2014
Edited: Nalini on 17 Feb 2014
Thank you very very much.. Your explanation is very lucid, also thank you for correcting the error in the code !! Thank you
Image Analyst
Image Analyst on 17 Feb 2014
You might try it like this:
% Create base file name.
baseFileName = sprintf('output%d.img', n);
% Specify folder where this file will reside.
folder = 'C:/temp/explicit_ball'; % Forward slashes work with Windows too.
% Combine the folder and the base file name to get the full file name.
fullFileName = fullfile(folder, baseFileName);
% Print info to the command window.
fprintf('Writing to file: %s ...\n', fullFileName);
% Open the file in binary mode, and get a file handle.
fileHandle = fopen('outfile','w');
% Write numbers out in binary.
count = fwrite(fileHandle, phi, 'float');
% Close the file.
status = fclose(fileHandle);
% Alert user it's done.
fprintf('Successully finished writing to file: %s\n', fullFileName);
At least that's how I'd do it. You might want to check if the file exists already and warn the user, and you might want to wrap it in try/catch:
try
% Put code here...
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!