Matlab GUI: problem with variables definition and multiple sliders
Mostrar comentarios más antiguos
Hi everyone. I have an .m file where at the end a GUI is launched. I built it using guide. I use a function UIGETVARIABLES ( https://es.mathworks.com/matlabcentral/fileexchange/37679-uigetvariables--dialog-to-pass-variables-from-workspace-into-gui ) as a dialogue interface to pass 4 variables into the GUI. Said 4 variables correctly place themselves into 4 edit-text features.
Now come the problems: when I use the sliders to change the variable values, I get an error saying that those variables are undefined.
Before reading mixed opinions about it I was using 'evalin' and it would let me use the sliders without errors, then though the unitary increase/decrease step would be applied correctly only to the first variable (v1). The other sliders (for v2,v3,v4) would use the same value as v1, so if e.g. initial value for v1 is 8 and for v2 is 5, an increase step for v2 would give me 9 and a decrease would give me 7! How can I change my code in order to get congruent results?
Another thing.
Once the sliders will work ok, I have a 'done' button to save the final values for the variables back into the m.file. I'm using:
assignin('base','v1',v1);
assignin('base','v2',v2);
assignin('base','v3',v3);
assignin('base','v4',v4);
Is this correct? Thanks.
Respuestas (1)
Geoff Hayes
el 17 de Nov. de 2017
giacomo - so it is the pushbutton9_Callback that calls the uigetvariables function so that you can get the v1, v2, v3, and v4 variables from your workspace.
v1 = set(handles.value1,'String',num2str(cell2mat(tvar(2))));
v2 = set(handles.value2,'String',num2str(cell2mat(tvar(2))));
v3 = set(handles.value3,'String',num2str(cell2mat(tvar(3))));
v4 = set(handles.value4,'String',num2str(cell2mat(tvar(4))));
I've removed the comments from the callback and so the above code is what you have left. Note that for v1 you are referencing tvar(2) when you probably want to use tvar(1) instead. I don't really understand what you are expecting to happen with this code. Shouldn't v1, v2, etc. be the values from tvar?
v1 = cell2mat(tvar(1));
v2 = cell2mat(tvar(2));
v3 = cell2mat(tvar(3));
v4 = cell2mat(tvar(4));
I'm not sure what they would be with your code (some sort of struct?). Now if you want to keep these values and/or update them, then save them to the handles structure instead as
handles.v1 = cell2mat(tvar(1));
handles.v2 = cell2mat(tvar(2));
handles.v3 = cell2mat(tvar(3));
handles.v4 = cell2mat(tvar(4));
guidata(hObject, handles);
We call guidata to save the updated handles structure so that all other callbacks get access to this handles structure with the new variables.
Start with the above and see if that helps!
6 comentarios
giacomo
el 17 de Nov. de 2017
Geoff Hayes
el 17 de Nov. de 2017
so when do you change v1? The above code resets the value of the slider to v1
set(handles.slider1, 'Value', handles.v1);
I must be misunderstanding what you are trying to accomplish with these v* variables.
You also don't need to reset the min, max, and slider step on every call slider1ContValCallback. I suspect that you just want to do this once (perhaps in the OpeningFcn of your GUI) and then set the
set(handles.slider1, 'Value', handles.v1);
in the pushbutton9_Callback when you read v1 (and the same for the other three sliders).
giacomo
el 20 de Nov. de 2017
Jan
el 20 de Nov. de 2017
@giacomo: It is a bad programming style to create variables in another workspace. assignin suffers from the same problems as globals and eval. See http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It is much better to store the variables locally in the figure's ApplicationData and start from a callback, what you currently perform in the command window (the base workspace).
giacomo
el 20 de Nov. de 2017
giacomo
el 20 de Nov. de 2017
Categorías
Más información sobre Scripts en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!