How do I get output arguments from a function without calling it (not as ans)
12 ビュー (過去 30 日間)
古いコメントを表示
Hi All, I require the output arguments of a function without calling them from the command line. For example: function is
function [R X Y Z l p] = tplot(i)
Is it possible to get the output (the variables R,X,Y,Z,l,p) in the workspace without calling them? Like instead of using
[R X Y Z l p] = tplot(3);
I just want to use tplot(3). I tried this but it stores the output as 'ans' .
Thanks, Bharat
1 件のコメント
Stephen23
2015 年 3 月 11 日
編集済み: Stephen23
2015 年 3 月 11 日
Why do this?
Bad programming like this will eventually lead to other questions on MATLAB Answers: "my code is buggy and I can't figure out why..."
Multiple very experienced users have all said in their answers "Do not do this". As cyclist suggests, you could store the outputs in a structure instead, if you really want to minimize typing.
採用された回答
Guillaume
2015 年 3 月 11 日
It can be done by using assignin in the function. It's on par with using global variables, goto, eval, etc. It makes the behaviour of your program a lot more obscure, so best avoided. Saving you from typing a few character is certainly not reason enough.
3 件のコメント
Guillaume
2015 年 3 月 11 日
Still as John and I warns, it's terrible practice. It may do what you want... until you or somebody else integrate the code in something else and ends up you/they end up spending hours debugging due to the unexpected behaviour.
その他の回答 (3 件)
Ortinomax
2015 年 3 月 11 日
I'm not 100% sure but I don't think this is possible and this is better.
I mean, imagine you already have the variables R,X,Y,... then you call the function. This variables exist inside the function workspace but don't outside.
One other thing, if your change your m-file to get a script. And if you run this script from command line (and the variable i already existed in the workspace). You will have variables R,X,Y,... defined in the workspace.
2 件のコメント
Ortinomax
2015 年 3 月 11 日
編集済み: Ortinomax
2015 年 3 月 11 日
Well, I read your answers and don't understand well what you have. I assume that :
At t=0. In your workspace you have R,X,Y,.... and a big matrix.
At t=1. You write tplot(3). tplot is executed using the matrix and variables R,X,Y...
At t=2. You want to have updated R,X,Y,... in your workspace.
To achieve this, you can put uglies things like
global R,X,Y,Z,I,P,youMatrix
in your function to give it access to your workspace variables. But no it's bad.
the cyclist
2015 年 3 月 11 日
It is not very clear what exactly you want to happen.
My best guess at something that will do close to what you want is the following
- Inside your function, store all of your outputs into a structure
- In the calling function, either call it with no output arguments (in which case ans will be a structure, with all your variables in it, or call it like S = tplot(3) , in which case S will be the output structure.)
2 件のコメント
the cyclist
2015 年 3 月 11 日
It is not the same. In your example, with
[R X Y Z l p] = tplot(i)
then ans will store only the single variable R. But if you create a structure S storing all the variables, then you will get all of them
John D'Errico
2015 年 3 月 11 日
編集済み: John D'Errico
2015 年 3 月 11 日
I did write the function putvar, expressly for the purpose. In general, it is not a good thing to get used to using. putvar was really designed to be used if you are debugging code, and want to save a variable to the base workspace in the middle of a debug session. However, putvar and uigetvar are on the file exchange.
However, if your goal is just that you don't want stuff stored as ans when you call the function, then there are several ways of avoiding that event, WITHOUT needing to use putvar.
In your function, IF you set it up so that it checks to see if there are no output arguments, then have it delete them, INSIDE the function. Essentially, you will add this block of code to the VERY end of your function:
if nargout == 0
clear R X Y Z l p
end
Now, when you call the function as
tplot(3)
NOTHING will appear in your workspace, not even ans.
This way, if you want an output, you get it. If you don't ask for one, NOTHING is stored.
(Actually, I think you can get away with just clearing R, since the other return variables are not put into ans in that case.)
Finally, there is one other way to solve the problem. You could just call your function like this:
[~] = tplot(3);
when you desire no output. Again, nothing extra will be stored, even if you do not put that block of code into the end as I suggested.
3 件のコメント
John D'Errico
2015 年 3 月 11 日
編集済み: John D'Errico
2015 年 3 月 11 日
Ugh. Well, that IS doable using putvar. I'd suggest it is dangerous (bad!) programming practice though.
You would put a test at the end of your code, based on nargout as I showed how to do in my answer. Only this time in the case of no outputs, then you would use my putvar tool to forcibly assign those variables.
Again, this is terribly bad programming practice. Why? Because it will make code incredibly difficult to debug, overwriting variables in the base workspace. That is especially true if someone else takes over your code someday, or you give it away. They will not expect to see a completely non-standard action like this happening.
And of course, if you called this function from a different function, then the assigning will occur in the base workspace, not in the function workspace. (I don't recall if I set up putvar to allow assignments to the caller workspace or not.)
Personally, I'd suggest that you either just use the syntax where it returns all variables, then since you don't want to be forced to retype the line but must do so often, just use the up-arrow key to recall that line from history. Or paste in that line from the clipboard. Or use the history window to paste in that line. I suppose you could also write a little script, that would execute the function, as well as have it return those variables. So there seem like many ways you can get what you want and not use the putvar (or assignin, since that is what putvar employs) solution.
Stephen23
2015 年 3 月 11 日
編集済み: Stephen23
2015 年 3 月 11 日
@Bharat: although you say that "I want the output to be same as when I do this: [R X Y Z l p] = tplot(3) or tplot(3)", this is not actually the same thing: the names of the assigned variables can be different to the ones used internally in the function tplot. This is a poor idea, and you should avoid doing this.
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!