writing vector element to file in .m files (via fprintf) produces no value while same command in terminal window does.

2 ビュー (過去 30 日間)
I have a .m file that uses fprintf to write text and a value to a file. Here are the statements (numOutputs = 1):
fprintf(fidResults,'\n\t\t%15s\t','ABS VAL OF DIFF');
for j = 1: numOutputs
fprintf(fidResults,'%10s: %16.2e',fieldval.outputName{j},diff(j));
end
fprintf(fidResults,'\n\t\t%15s\t','PCNT DIFF');
for j = 1: numOutputs
fprintf(fidResults,'%10s: %16.2e',fieldval.outputName{j},Pcntdiff(j));
end
Looking at the file that is written to I see:
ABS VAL OF DIFF BESSJ:
PCNT DIFF BESSJ: -6.37e-03
The first value, diff(j) is not being printed. If I pause at this line, I see that diff(j) does exist. If at this breakpoint, I manually hightlight thes same command, rightclick and select "Evaluate Selection" (ie if I execute the very same command at the prompt) the value is written. How is it that the exact same command executed at the exact same place in the program, one by terminal prompt and the other via the script, do not work??

採用された回答

Carl Loeffel
Carl Loeffel 2022 年 5 月 12 日
I figured it out. Really stupid of me to use "diff" as a variable name. "diff" is already taken as a matlab functions. Upon renaming to "delta" it worked fine.

その他の回答 (1 件)

Carl Loeffel
Carl Loeffel 2022 年 5 月 12 日
That's the thing. It is not empty when I stop at that line using a breatk point. And executing that line manually, fprintf(fidResults,'%10s: %16.2e',fieldval.outputName{j},diff(j)); at the prompt in the command window does write it out correctly. Only when I execute that line via running the .m file that it resides in does it become empty..

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by