How to code this command in gui?
3 views (last 30 days)
Show older comments
Jonathan O'Neill
on 26 Aug 2014
Commented: Geoff Hayes
on 28 Aug 2014
I have a function I am working on, "SetImage"
[ret] = SetImage(hbin, vbin, hstart, hend, vstart, vend)
I am making a gui that lets you enter each of these parameters into text boxes, once the "set image" button is clicked it will read the data I have typed into the text boxes and use them for each parameter in this function...
I am unaware of how I can set each box to correspond to each individual parameter, this is my attempt to do so:
% --- Executes on button press in setimagebutton.
function setimagebutton_Callback(hObject, eventdata, handles)
% hObject handle to setimagebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
setimgp1=('handles.setimgh'),('String')
setimgp2=('handles.setimgv'),('String')
setimgp3=('handles.setimgstartc'),('String')
setimgp4=('handles.setimgendc'),('String')
setimgp5=('handles.setimgstartrow'),('String')
setimgp6=('handles.setimgendrow'),('String')
[ret]=SetImage(setimgp1,setimgp2,setimgp3,setimgp4,setimgp5,setimgp6);
The handles are the tags of each of my edit boxes, when I try to run it I receive the error message:
Error using atmcdmex SetImage- parameter 6 is not a number
Error in SetImage (line 25) [ret] = atmcdmex('SetImage', hbin, vbin, hstart, hend, vstart, vend);
Error in CamTestGUI>setimagebutton_Callback (line 1866) [ret]=SetImage(setimgp1,setimgp2,setimgp3,setimgp4,setimgp5,setimgp6);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in CamTestGUI (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)CamTestGUI('setimagebutton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
0 Comments
Accepted Answer
Geoff Hayes
on 27 Aug 2014
Jonathan - you almost have the correct code. You will need to use the get function in order to get the string data from the widget, and then save it to the variable. But since the variables appear to be numeric in nature, you will have to convert them from a string to a number
setimgp1=str2num(char(get(handles.setimgh,'String')));
setimgp2=str2num(char(get(handles.setimgv,'String')));
setimgp3=str2num(char(get(handles.setimgstartc,'String')));
setimgp4=str2num(char(get(handles.setimgendc,'String')));
setimgp5=str2num(char(get(handles.setimgstartrow,'String')));
setimgp6=str2num(char(get(handles.setimgendrow,'String')));
The char function is used to convert the result of the get from a cell to an array of characters (a string). You may or may not need this, but it can't hurt.
Note that if any of the widgets have non-numeric text, then the above conversions may result in an empty matrix being assigned to one of the variables. You may want to add some logic in your code to guard against this happening.
Try the above and see what happens!
7 Comments
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!