필터 지우기
필터 지우기

Please tell me what is the outfile in this code

조회 수: 4 (최근 30일)
Nalini
Nalini 2014년 2월 17일
댓글: Image Analyst 2014년 2월 17일
[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