How can I access the variables in base workspace in GUI?

16 ビュー (過去 30 日間)
dyyyyyl
dyyyyyl 2016 年 3 月 9 日
コメント済み: Stephen23 2016 年 3 月 9 日
Hello there,
I'm working on a GUI that needs to access some variables in base workspace. I want to find all the variables that start with 'signal_' in base workspace, so my code is:
all_signals=who('signal_*'); % find all the variables that start with 'signal_' and the output is a cell
But the problem is that GUI has its own workspace and cannot access the variables in base. I've tried evalin, which I know is not recommended, and I got this error: Undefined function or method 'evalin' for input arguments of type 'cell'
And then I tried char(), but the output is just character.
So is there any other idea how to solve this problem? Thanks in advance.
  1 件のコメント
Stephen23
Stephen23 2016 年 3 月 9 日
As you are discovering using evalin is an incredibly buggy way to pass data to the base workspace. It is also very slow and obfuscated. Read the documentation and you will find that evalin is the least recommended way to pass data between workspace. Just pass the data as arguments, and your code will be much more robust.
And of course evalin suffers from all of the disadvantages of eval, sadly commonly used by beginners who don't mind writing buggy, slow, and obfuscated code:

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

採用された回答

Ilham Hardy
Ilham Hardy 2016 年 3 月 9 日
This might work,
evalin('base','temp = who(''signal_*'',''base'');assignin(''caller'',''all_signals'',temp)');
Hope that helps,
Ilham
  2 件のコメント
dyyyyyl
dyyyyyl 2016 年 3 月 9 日
hello, thx for your reply. Yet I have two questions: 1. Why do you use double quotation marks? shouldn't it be single? 2. Actually I've tried evalin('base', 'who('signal_*')') before, which is almost the same with your first line. But whether yours or mine will get this error: Invalid syntax at 'signal_'. Possibly, a ),} or ] is missing. But I don't think it's missing something.. Do you have any idea?
Ilham Hardy
Ilham Hardy 2016 年 3 月 9 日
編集済み: Ilham Hardy 2016 年 3 月 9 日
  1. The second argument of evalin is inside the single quote. Any quotes required within the single quote (second argument) must be represented by double quotes. Otherwise you will get syntax error..
  2. Are you sure you are not missing any quotes or characters? I just tried the code I posted and it working properly. What if you put a breakpoint in your function/GUI, and then copy/paste and execute the above code in the Command Window?

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by