Matlab GUI: problem with variables definition and multiple sliders
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Geoff Hayes
2017-11-17
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!