Problem with GUI variables from workspace
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jakub F
am 28 Aug. 2016
Bearbeitet: Stephen23
am 28 Aug. 2016
Hello, I have a problem running my GUI. I am defining bunch of variables using "Edit Text". I am signing them to workspace using
assignin('base','variable_name',value);
Then I want to calculate some new variables using those defined in Workspace after Button in GUI is pressed, but when i press the Button I am getting an error that the variable which I want to use in calculation is not defined even though i can see this exact variable visible in Workspace.
The simplified code goes like this:
function zwrot_tp_o_Callback(hObject, eventdata, handles)
zwrot_tp_o=str2num(get(handles.zwrot_tp_o,'String'));
assignin('base','zwrot_tp_o',zwrot_tp_o);
the same way as shown above I am defining other variables. Then the Button:
function symuluj_Callback(hObject, eventdata, handles)
tmax=1000;
t= [1:tmax]';
x1= zeros(zwrot_tp_o-1,1);
x2= ster_o * ones(zwrot_tk_o - zwrot_tp_o,1);
x3= zeros(tmax-zwrot_tk_o,1);
x=vertcat(x1,x2,x3);
The error occurs in line where I calculate x1, it says that i have undefined "zwrot_tp_o" even though it is in workspace. So I tried defining that variable once again just above the x1 equation just like: zwrot_tp_o=500 and then it was accepting this variable but was giving the same error about next variable. Any ideas?
1 Kommentar
Stephen23
am 28 Aug. 2016
Bearbeitet: Stephen23
am 28 Aug. 2016
Rather than learning bad habits like assignin and evalin and global, why not learn how to program using fast, reliable and robust ways of passing data between workspaces?
Every beginner has a choice: to learn how to program well, or to write hack code that will cause more problems than it solves (e.g. your example code).
Read these to start learning how to write better code:
Akzeptierte Antwort
Walter Roberson
am 28 Aug. 2016
You have confused the 'base' workspace as being some kind of "global" workspace. Variables that are defined in the base workspace need to be retrieved from the base workspace using evalin('base') .
Assigning in base and using global are not recommended. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Variables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!