Please tell me what is the outfile in this code

4 次查看(过去 30 天)
[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

采纳的回答

Walter Roberson
Walter Roberson 2014-2-17
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 个评论
Nalini
Nalini 2014-2-17
编辑:Nalini 2014-2-17
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 2014-2-17
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by