Is there a way to save a variable created in a function to the workspace?

I have a variable being created in a function and want to save in the workspace so the command window can access.

 採用された回答

Oleg Komarov
Oleg Komarov 2012 年 3 月 19 日
the syntax of a function could be summarized as:
[output1, output2, ...] = function(input1, input2, ...)
Say, yuor variable is named X in the function, then all you need to do is:
X = function(...)
If you call the function as:
function(...)
your variable will be assigned automatically to ans, otherwise to
anyname = function(...)
anyname will be what is X in your function.
As Aldin pointed out, if you're in a GUI then use:
assignin('base','someName',X)

その他の回答 (2 件)

Aldin
Aldin 2012 年 3 月 19 日

0 投票

If you working in GUI i THINK that's impossible. It is an mistake in MATLAB because the data are not permanent as in Java.

2 件のコメント

Oleg Komarov
Oleg Komarov 2012 年 3 月 19 日
generally, to swap variable from any specific workspace to the base one you can use:
assignin('base','someName',yourvar)
Aldin
Aldin 2012 年 3 月 19 日
Hi Oleg. Sorry for my behavior. Thank you. I didn't know that.I think It would be better for MATLAB to think object oriented.

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

Sahyadri Vibhu
Sahyadri Vibhu 2012 年 3 月 20 日
I think "persistent" will help.
Just use
persistent variable_name = value

1 件のコメント

Walter Roberson
Walter Roberson 2012 年 3 月 20 日
That is not valid syntax. You can use
persistent variable_name
to indicate that the variable keeps its value between calls. You cannot, though, initialize it in the same statement, and doing so would be a waste of the idea that you want to pull back the value the variable had in the last function call.

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

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by