Problem with GUI variables from workspace

2 vues (au cours des 30 derniers jours)
Jakub F
Jakub F le 28 Août 2016
Modifié(e) : Stephen23 le 28 Août 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 commentaire
Stephen23
Stephen23 le 28 Août 2016
Modifié(e) : Stephen23 le 28 Août 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:

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Août 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') .

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by