How to code this command in gui?

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

 Accepted Answer

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

Hi Geoff, thanks for the reply!
_
_% --- Executes on selection change in SetImage.
function SetImage_Callback(hObject, eventdata, handles)
% hObject handle to SetImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns SetImage contents as cell array
% contents{get(hObject,'Value')} returns selected item from SetImage
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')));
[ret]=SetImage(setimgp1,setimgp2,setimgp3,setimgp4,setimgp5,setimgp6);_ _
I have replaced my code with yours but I still get an error when I enter some numbers into the boxes and click on "setimage".
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 1889) 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
I'm not too sure what you mean about adding some logic into the code to prevent an empty matrix being assigned to one of the variables, can this be the cause of the error?
Thanks for the help!
Hi Jonathan - what is the function signature for atmcdmex? I think that the error message is telling you that the sixth parameter, vstart, is not a number and so must be some other data type. This must be a custom function so you will have to step through this code to determine what is being passed and what should be being passed. Just look in the code for the logic that tests for whether a parameter is a number of not.
What is vstart? How is it calculated/determined within SetImage?
As for guarding against empty matrices, no, I don't think that is the source of the error. But you should see what happens when an user enters non-numeric characters in one of your edit fields. Put a breakpoint in your SetImage_Callback and step through the code to see what happens.
atmcdmex is an "undocumented function" so I am unable to find out exactly what it is... vstart is the start row for the binning, maybe its a co-ordinate?
hbin: number of pixels to bin horizontally.
vbin: number of pixels to bin vertically.
hstart: Start column (inclusive).
hend: End column (inclusive).
vstart: Start row (inclusive).
vend: End row (inclusive).
It isn't calculated within SetImage I just enter what it is into the edit box, and it should update to this value.(see image in first post)
[ret]=SetImage(setimgp1,setimgp2,setimgp3,setimgp4,setimgp5,setimgp6);
this replaces the standard code:
[ret] = SetImage(hbin, vbin, hstart, hend, vstart, vend)
I have just realised that I might need to define the variables again... ie: hbin=setimgp1, vbin=setimgp2 etc...
Im not too sure what the breaks are for, do I put it in halfway through the code to try and find where it is failing?
Breakpoints allow you to debug the software. See Doug's video here on how to use the debugger. It is very helpful!
So if you put a breakpoint at line 27 in SetImage when the following is executed
atmcdmex('SetImage', hbin, vbin, hstart, hend, vstart, vend);
what can you say about each variable? In the Command Window, type the following and record the results
class(hbin)
class(vbin)
class(hstart)
class(hend)
class(start)
class(vend)
The above statements will return the data type for each of these inputs.
I'm not sure what you mean by atmcdmex is an "undocumented function". Do you have the function and there are just no comments? Can you open the function in the MATLAB editor and look for the code that is generating the _ parameter 6 is not a number_ error? (You may only find the text is not a number if the code is smartly written.)
EDIT
Based on the name of the function, atmcdmex, it must be a MEX-function, and so was C-code (or Fortran, C++) that has been built into a MEX-function.
Can you attach the code for SetImage.m so I can see how your inputs are translated into the inputs for the atcdmex function? Or just post the relevant code if you'd rather.
This is odd, when I type each of these into the command box this returns... >> class(hbin) Undefined function or variable 'hbin'.
>> class(vbin) Undefined function or variable 'vbin'.
>> class(hstart) Undefined function or variable 'hstart'.
>> class(hend) Undefined function or variable 'hend'.
>> class(start) Undefined function or variable 'start'.
>> class(vend class(vend | Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
>> class(vend) Undefined function or variable 'vend'.
I am writing the code for the "setimage" command in the m file for my gui so I thought these would work ok.
When I click on atmcdmex in the command window this is what appears:
When I open Setimage.m this is all there is
How can I open the function in the matlab editor to look at the code generating parameter 6 is not a number? Thanks for you help I haven't a whole lot of experience with guis on matlab, I will watch that video now
Thanks for all the help Geoff! I have it going now, you had it sorted for me in your earlier posts but I had been editing a redundant "setimage" function in my mfile! So when I clicked "set image" it was running my old code anyway haha.
Sorry about that and thanks for all your help.
Glad that it worked out!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!