Matlab GUI: problem with variables definition and multiple sliders

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)

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

Yes, pushbutton9_Callback both loads an image to handles.axes1 and the 4 variables into the 4 edit-text features.
Yes, tvar stores 4 variables from the main and loads them into the GUI interface. v1,v2,v3,v4 are [1x1 double], while tvar is the cell array storing them.
I changed the code as you said, then in the sliderCountVal callback I changed the handles.slider1 value to handles.v1, but then when I try to use the slider it won't increase/decrease the results. I don't even get an error, it just won't let me do the update with the slider.
% --- Scrolling on the slider and update the results
function slider1ContValCallback(hFigure,eventdata)
% test it out - get the handles object and write the current value
% to the edit box
handles = guidata(hFigure);
sliderValue = get(handles.slider1,'Value');
set(handles.value1,'String',num2str(sliderValue));
set(handles.slider1, 'Min', 1);
set(handles.slider1, 'Max', 100);
set(handles.slider1, 'SliderStep', [1/99, 1]);
set(handles.slider1, 'Value', handles.v1);
and same for the other 3 text-edit features.
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).
Hi. To make this a little bit easier I just created other 4 edit-text features where I can manually change the results and save them to workspace. I think I'll use this instead of the slider. I have a problem with the 'final' variables (nunof, nduef, ntref, nquattrof) that are assigned pressing a 'done' button:
function done_Callback(hObject, eventdata, handles)
% hObject handle to done (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
assignin('base','nunof', str2num(char(get(handles.final1,'String'))));
assignin('base','nduef', str2num(char(get(handles.final2,'String'))));
assignin('base','ntref', str2num(char(get(handles.final3,'String'))));
assignin('base','nquattrof', str2num(char(get(handles.final4,'String'))));
I have called the GUI interface in a m.file, but the above variables are correctly assigned to the workspace, still when I run the m.file it tells me they are undefined. what am I doing wrong? I just need to take the new values and use them for some equations after I called and used the GUI part. Thanks.
@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).
Hi Jan. Yes I read about it basically everywhere in this forum. I just use evalin to print values into the first 4 text edits, as I don't really need to do anything with them, just display them. Then I'm trying to just save the final text edit values in a way that could be seen by the m. file the GUI is called in. I read a bunch of different blogs and answers but it's really hard for a new user to get something out of that tbh, at least for me. Each little step has me stuck for half a day/whole day.
I tried not to use 'evalin' to read my values in the GUI.
I created a n.mat file in the original m.file where I load in the variables I need to show/save.
n = [nuno, ndue, ntre, nquattro];
save n.mat
Then in the GUI 'upload initial values' button I assign these values to the initial text edits like this:
S = load('n.mat');
handles.v1 = S.nuno;
handles.v2 = S.ndue;
handles.v3 = S.ntre;
handles.v4 = S.nquattro;
set(handles.initial1,'String',num2str(handles.v1));
set(handles.initial2,'String',num2str(handles.v2));
set(handles.initial3,'String',num2str(handles.v3));
set(handles.initial4,'String',num2str(handles.v4));
guidata(hObject, handles);
Then I have other 4 edittext features where I digit manually some updated values and save them using a 'done button':
here, how can I save the final values in the same 'n.mat' file with the same names (nuno, ndue....)? I'm basically just overwriting the values as an update.

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Nov. de 2017

Comentada:

el 20 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by