Hi everybody, I'm trying to edit this output function within the command fopen and fprintf:
function stop=outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
fID2=fopen(['I_vs_texp--',datestr(now,'dd_mm_yyyy_HH_MM_SS') '.txt'],'a');
case 'iter'
fprintf(fID2,'%6.2f %12.8f\r\n',{optimValues.iteration, optimValues.resnorm, x(1),x(2),x(3)});
case 'done'
fclose ('all')
end
, but something must be wrong with the name given to the new file, because it gives me the following:
Undefined function or variable 'fID2'.
Thanks !!

댓글 수: 2

Adam
Adam 2018년 6월 13일
Well, you define it in one case of a switch statement and then access it in a mutually exclusive one so in the state where you define it you don't use it and in the state where you try to use it you don't define it.
Sergio Quesada
Sergio Quesada 2018년 6월 13일
편집: Sergio Quesada 2018년 6월 13일
But if i define it on the 'init' statement, it will create one different file after each iterarion...

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

 채택된 답변

Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일

0 개 추천

Add this line at the start of the function:
persistent FiD2
Each time the function is called it has no recollection of what happened last time it was called, unless you explicitly give it some way to remember: that is exactly what persistent is for, so that the next time the function is called, it will remember that variable. Note that in general it is a bad practice to call fclose('all') like that, except perhaps from the command line.
function stop=outfun(x,optimValues,state)
persistent fid
stop = false;
switch state
case 'init'
tmp = datestr(now,'dd_mm_yyyy_HH_MM_SS');
tmp = sprintf('I_vs_texp--%s.txt',tmp);
fid = fopen(tmp,'a');
case 'iter'
fprintf(fid,'%6.2f %12.8f\r\n',{optimValues.iteration, optimValues.resnorm, x(1),x(2),x(3)});
case 'done'
fclose(fid)
end

댓글 수: 6

right, I think it works... Now it gives the following message:
Error using fprintf
Function is not defined for 'cell' inputs.
Maybe because the FormatSpec of fprintf is not correct (?). The iteration displayed have this format:
[3] [0.0720] [1.0464] [2.1608] [4.9719]
So I am writing
fprintf(fID_I_vs_texp,'%d %d %d %d\r\n',{optimValues.iteration, optimValues.resnorm, x(1),x(2),x(3)});
Is this correct ??
Thanks again !!
Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일
As the error message says, inputs to fprintf (and sprintf) cannot be cell array. But it can have multiple inputs. So you probably want something like this:
fmt = '%d %d %d %d\r\n'
fprintf(fid,fmt, optimValues.iteration, optimValues.resnorm, x(1),x(2))
You put six items into that cell array, but only have four conversion specifiers in the format string: I leave that for you to reconcile. I recommend that you use the debugging tool (i.e. set a breakpoint) and then manually call fprintf with those variables, until you get the output that you require.
Adam
Adam 2018년 6월 13일
편집: Adam 2018년 6월 13일
The arguments to fprintf should not be in a cell array, they should just each be their own argument as in the documentation where they are the equivalent of A1,...An
doc fprintf
Sergio Quesada
Sergio Quesada 2018년 6월 13일
편집: Sergio Quesada 2018년 6월 13일
I don't understand... It is the same syntax that this example:
fprintf(fileID,'%6.2f %12.8f\r\n',A);
( actually taken from doc fprintf), and this latter example do works...
Adam
Adam 2018년 6월 13일
A is not a cell array in that case though, it is a numeric array.
Sergio Quesada
Sergio Quesada 2018년 6월 13일
oh, ok. Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 6월 13일

댓글:

2018년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by