How to read the result of fprintf

4 ビュー (過去 30 日間)
Dmytro Sokol
Dmytro Sokol 2023 年 1 月 31 日
コメント済み: Walter Roberson 2023 年 1 月 31 日
I have the following code line:
fprintf('set %d\n', ID);
That ID is not directly accessible to me, so I can't simply use it. However, if I find such text file I am able to use its content.
So, where can I find a text file with the data (1) and how can I read that ID (2)?
For example,
assignin('base', 'id', ID);
works perfect for my purposes. So I hope it is possible to use similar approach for fprintf.
  3 件のコメント
Adam Danz
Adam Danz 2023 年 1 月 31 日
fprintf doesn't write to a variable. It writes to a file or the command winodw. Are you thinking of sprintf?
Dmytro Sokol
Dmytro Sokol 2023 年 1 月 31 日
Sorry for the confusion. I've provided a more correct statement of the problem.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 1 月 31 日
See evalc() or sometimes diary()
But you would typically be better off using sprintf or compose
  2 件のコメント
Dmytro Sokol
Dmytro Sokol 2023 年 1 月 31 日
Unfortunately, I am not able to replace fprintf with smth else
Walter Roberson
Walter Roberson 2023 年 1 月 31 日
output = evalc("simout = sim('model', appropriate parameters);");

サインインしてコメントする。

その他の回答 (2 件)

Steven Lord
Steven Lord 2023 年 1 月 31 日
ID = 42;
fprintf('set %d\n', ID);
set 42
When called with that syntax, fprintf does not write to a file. It writes to the Command Window. Unless you're already capturing the Command Window output using diary or the -logfile startup option or something similar or if you have a pair of fprintf calls (one that displays to the Command Window and one that writes to a file) you can't retrieve that information automatically.
  3 件のコメント
Walter Roberson
Walter Roberson 2023 年 1 月 31 日
Steven Lord
Steven Lord 2023 年 1 月 31 日
Where does ID come from in that case? Is it stored in a model workspace? Is it an output from the function block that you can send to a To Workspace block? Does it come into the function block from another block (which can be connected to a To Workspace block?)

サインインしてコメントする。


Image Analyst
Image Analyst 2023 年 1 月 31 日
The result of that fprintf() would be a line printed to the command window. Because you did not provide a file handle as the first argument to fprintf(), that means it will go to the command window instead of to a file on disk. ID must somehow be a variable that is visible to the function where you are calling fprintf(). Like
ID = 123;
fprintf('set %d\n', ID); % Display "set 123" in the command window.
Like Steve said.
" if I find such text file" <=== I have no idea what text file you are referring to.
And I have no idea how to use Simulink.

カテゴリ

Help Center および File ExchangeString についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by