How do i use the edit text box in a GUI to change variables in a function that i run with the gui???
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Richard
am 12 Jun. 2014
Kommentiert: Geoff Hayes
am 13 Jun. 2014
Hello i am not good at matlab at all and i am trying to make a GUI work like i want it too. I want to be able to change values in my function from the GUI before I run the GUI. This is code i want to run in the GUI and the Ss and SR are the variables i want to be able to control from the GUI.
function AutofocusGUI(Ss , SR)
Ss=.01;
SR=5;
Fvar=[];
h.MoveAbsoluteEx (0,0,1,0); % home
pause(3)
for z = 0:Ss:SR % Step size and Sweep range
h.MoveAbsoluteEx (0,z,1,0);
pause(.75)
frame = getsnapshot(vid);
frame=frame(160:320, 240:400);
Fvar=[Fvar std2(im2double(frame))];
end
pause(2)
figure(3)
z = 0:Ss:SR
plot(z,Fvar) % graph of step and variance
xlabel('z')
ylabel('variance')
pause(2)
maxFvar=max(Fvar);
[mmaxFvar , ind] = max(Fvar);
maxX= z(ind);
h.MoveAbsoluteEx (0,maxX,1,0);
end
I was able to get the GUI to run AutofocusGUI(Ss, SR) code with the variables Ss and SR defined in the code and it worked great but i was not able to control Ss and SR. This is what i did.
% --- Executes on button press in StartAutofocus.
function StartAutofocus_Callback(hObject, eventdata, handles)
% hObject handle to StartAutofocus (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
AutofocusGUI(Ss,SR);
end
What should i do now to be able to control the values of Ss and SR from my edit text box in the GUI before I press the StartAutofocus button to start the function.
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 12 Jun. 2014
If we assume that your GUI has two edit text boxes, one for Ss and one for SR, then, in the above button callback, you can get the data from those two edit text boxes and pass them to the AutofocusGUI function
function StartAutofocus_Callback(hObject, eventdata, handles)
Ss = str2num(char(get(handles.editSs,'String')));
SR = str2num(char(get(handles.editSR,'String')));
In the above, the text edit boxes are named editSs and editSR. We convert the contents of each to a string, and then to a number. But we have to be aware of the user entering in non-numeric data - if either Ss or SR is empty, then we cannot proceed with the calculation
if ~isempty(Ss) && ~isempty(SR)
AutofocusGUI(Ss,SR);
end
(If one or both are empty, you may want to default either value to something that can be used.) And that should be it - just remember that your AutofocusGUI function is setting Ss and SR to 0.1 and 5 respectively so this code should be removed. Hope this helps!
0 Kommentare
Weitere Antworten (1)
Richard
am 13 Jun. 2014
3 Kommentare
Geoff Hayes
am 13 Jun. 2014
Awesome!
As for the char, I needed that in my version (2014a) because the result of the get(handles.editSs,'String') was a cell..so I needed to convert it to a string/char array. I haven't seen anyone else do that (in other pieces of code) so maybe it isn't necessary for you and is just something specific to the way I set something up.
Siehe auch
Kategorien
Mehr zu Scope Variables and Generate Names 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!