Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Accessing within a matlab function a string that duplicates the command that called the function.

1 回表示 (過去 30 日間)
Leo Simon
Leo Simon 2013 年 8 月 23 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Apologies for the horrible question title. It's easiest if I give an example of what I'm trying to ask. Suppose I have a function f that takes a lot of optional arguments. I might call it as follows:
f(x,'dog',1,'cat',2,'cow',3)
Presumably matlab retains a record of the string I just typed, because it has to pass it as a string to the history.m file. I'd like to be able to save this string to a mat file from within f, so that I can remember the exact way that I called f, and thus duplicate the rest of the data that's saved in the mat file.
Of course, I could reconstruct the string myself, building from varargin, etc, but this would be really messy, especially if the function call involved both optional and non-optional arguments.
Thanks!
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 8 月 23 日
When you are talking about "from within f" are you referring to when you are debugging ?

回答 (1 件)

Image Analyst
Image Analyst 2013 年 8 月 23 日
Perhaps the diary command will work, but I'm not sure if it will record the command as soon as it enters the function, or only after it has exited the function.
  2 件のコメント
Leo Simon
Leo Simon 2013 年 8 月 23 日
You'd have to remember to open the diary before you called the function. Which unfortunately I'd forget to do, just when I really needed to know what the call was
Image Analyst
Image Analyst 2013 年 8 月 23 日
編集済み: Image Analyst 2013 年 8 月 23 日
How can you forget? I just told you. Just type it in
delete(filename);
diary(filename);
f(x,.......
delete(filename); % Now get rid of it if you want it to be temporary.
Inside the f function:
fid = fopen(filename);
% read file with fgetl() or whatever, then exit.
Or you can just use fprintf() to print the input arguments to the command line or a file. Note: like I said, I don't know if the diary will contain the information at that point - you might have to wait until f exits.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by