How do i use the edit text box in a GUI to change variables in a function that i run with the gui???
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Geoff Hayes
2014-6-12
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 个评论
更多回答(1 个)
Richard
2014-6-13
3 个评论
Geoff Hayes
2014-6-13
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!