Clear Filters
Clear Filters

How do i use the edit text box in a GUI to change variables in a function that i run with the gui???

4 views (last 30 days)
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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 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!

More Answers (1)

Richard
Richard on 13 Jun 2014
Hello THANKS for the fast response I will give this a shot tomorrow in the lab i cant run the code with out the hardware :/ . This is what i tried earlier after i took the defined Ss and SR out of the AutofocusGUI function.
% --- 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)
InpStr = get(handles.Ss,'String');
Ss = str2double(InpStr);
InpStr = get(handles.SR,'String');
SR = str2double(I);
AutofocusGUI(Ss,SR);
end
When i pushed the Start Autofocus Button in the GUI i got back a error message that said:
"Attempt to reference field of non-structure arry"
Error in libsGui>StartAutofocus_Callback
InpStr = get(handles.Ss,'String');
Why do i get this error? Thanks A TON!!!!!
  3 Comments
Richard
Richard on 13 Jun 2014
This worked great Thanks!!!!!!!!!!
But what is the char doing to the handles to make this work?
Thanks again Rich
Geoff Hayes
Geoff Hayes on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!