Matlab GUI: problem with variables definition and multiple sliders

2 Ansichten (letzte 30 Tage)
giacomo
giacomo am 16 Nov. 2017
Kommentiert: giacomo am 20 Nov. 2017
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.

Antworten (1)

Geoff Hayes
Geoff Hayes am 17 Nov. 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 Kommentare
giacomo
giacomo am 20 Nov. 2017
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.
giacomo
giacomo am 20 Nov. 2017
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by