필터 지우기
필터 지우기

inputting an output file name into a function

조회 수: 2 (최근 30일)
Matthew
Matthew 2012년 12월 28일
HI,
I have a function:
function=tcaproc(mults,c2flag,filename)
where I do some math and then want to print to a file called filename.txt, for example, something like data1out.txt. I want to be able to change the filename is my point. So far I have tasted only abject failure.
formatSpec='%s.txt';
str=sprintf(formatSpec,filename)
file=fopen(str,'wt+');
just tells me that my filename doesn't exist.
test=tcaproc(as4,1,test1);
Undefined function or variable 'test1'.
I would appreciate your help.

채택된 답변

Image Analyst
Image Analyst 2012년 12월 28일
편집: Image Analyst 2012년 12월 28일
When you do this:
file=fopen(str,'wt+');
you're getting an ID number (a "handle") to the file. So you'd do this:
function tcaproc(mults,c2flag,filename)
try
formatSpec='%s.txt';
baseFileName = sprintf(formatSpec, filename)
fullFileName = fullfile(pwd, baseFileName);
fileHandle= fopen(fullFileName ,'wt+');
% More code...... such as fprintf(fileHandle, '%s', someText);
% Handle any errors:
catch ME
errorMessage = sprintf('Error in function tcaproc.\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
And you'd call it like this:
filenameInMainProgram = 'test';
tcaproc(multsInMainProgram, c2flagInMainProgram, filenameInMainProgram);

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 12월 28일
test=tcaproc(as4,1,'test1');
  댓글 수: 1
Matthew
Matthew 2013년 1월 2일
It was not quite that simple, but it was very close. The marked answer below was right on the money. THANKS TO ALL.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by