Problem with GUI variables from workspace

2 visualizaciones (últimos 30 días)
Jakub F
Jakub F el 28 de Ag. de 2016
Editada: Stephen23 el 28 de Ag. de 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 comentario
Stephen23
Stephen23 el 28 de Ag. de 2016
Editada: Stephen23 el 28 de Ag. de 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:

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Ag. de 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') .

Más respuestas (0)

Categorías

Más información sobre Variables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by