Check if variable exists in workspace to plot variable, else generate error

Hi there,
I am having some trouble with checking if a variable exists in the workspace. If the variable exists (which is a structure with time), it should plot the data in my GUI. If the variable does not exist, it should generate an error.
I used this code, however it will always generate an error even if the variable does exist in the workspace. I cannot find what I am doing wrong.
function AmechM_Callback(hObject, eventdata, handles)% Executes on button press in AmechM.
if exist('AmechM','var')
AmechM = evalin('base','AmechM');
plot(AmechM.time,AmechM.signals.values);
xlabel(handles.ax1,'time (s)');
ylabel(handles.ax1,'Acceleration (rad/s²)');
else
errordlg('The workspace does not contain any data. Run a simulation first.','Error');
end

2 件のコメント

per isakson
per isakson 2015 年 1 月 9 日
"exists in the workspace" &nbsp which workspace, that of the callback function or the base?
Maarten
Maarten 2015 年 1 月 9 日
The base workspace.

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

 採用された回答

per isakson
per isakson 2015 年 1 月 9 日
編集済み: per isakson 2015 年 1 月 9 日
replace
if exist('AmechM','var')
by
if exist('AmechM','var') == 1
or rather by
ise = evalin( 'base', 'exist(''AmechM'',''var'') == 1' )
if ise
since you want to know whether AmechM exists in the base workspace
and read the on-line help on exist and on "scope"

8 件のコメント

Maarten
Maarten 2015 年 1 月 9 日
編集済み: Maarten 2015 年 1 月 9 日
The code
ise = evalin( 'base', 'exist(''AmechM'',''var'') == 1' )
if ise
worked, thanks!
So for my own knowledge: using
if exist('AmechM','var') == 1
only checks the existence of the variable in the callback function?
per isakson
per isakson 2015 年 1 月 9 日
編集済み: per isakson 2015 年 1 月 9 日
"but this still does not work though" &nbsp what do you mean?
Is ise true or false when the variable exists in the base workspace?
Maarten
Maarten 2015 年 1 月 9 日
I did not see that you edited your answer before I commented. Thanks.
per isakson
per isakson 2015 年 1 月 9 日
per isakson
per isakson 2015 年 1 月 9 日
... and I answered without reading the whole question. Happy my answer helped you.
Aditya Kaki
Aditya Kaki 2018 年 9 月 26 日
The above solution works well for variables. but its failing in detecting structure variables. eg: exist(['report.f_fun_redundant{' m '}'],'var')
If any of you have a solution could you please share . Thanks in advance.
Moritz Plinke
Moritz Plinke 2020 年 2 月 14 日
編集済み: Moritz Plinke 2020 年 2 月 14 日
I know answer is really late but I struggled on a very similar case. My solution is using the function fieldnames().
contains( fieldnames( variable_to_inspect) , 'sub_variable_name_string' )
To make it suitable and robust for "if", use sum()
sum( contains( fieldnames( variable_to_inspect ) , 'sub_variable_name_string' ) )
Alex Morgan
Alex Morgan 2022 年 8 月 16 日
Yes, I think fieldnames is the best solution for structures

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2015 年 1 月 9 日

コメント済み:

2022 年 8 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by