Can someone talk me through making matlab use values from a popup box when I press a button?

Hi, I have created a GUI and am trying to tidy up my code to make changes easier. Basically I have a button that runs my code, which I set all of my variables/settings etc.. within. Instead of having this list of code that a user needs to edit for a certain test I made a GUI with popup menus in order to select them. Can someone explain to me how I go about using the settings picked in my popup menu within my main "previmage" button?
example:
% --- Executes on button press in previmage.
function previmage_Callback(hObject, eventdata, handles)
% hObject handle to previmage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp('iCam Live Mode Preview')
PrepareAcquisition()
% init system
disp('Initialising Camera');
ret=AndorInitialize('');
CheckError(ret);
disp('Configuring Acquisition');
[ret]=CoolerON(); % Turn on temperature cooler
CheckWarning(ret);
[ret]=SetAcquisitionMode(5); % Set acquisition mode; 5 for RTA
CheckWarning(ret);
[ret]=SetExposureTime(0.02); % Set exposure time in second
CheckWarning(ret);
[ret]=SetReadMode(4); ****************************%Set read mode***********************
CheckWarning(ret);
[ret]=SetTriggerMode(10); % Set Software trigger mode
useSoftwareTrigger = true;
As you can see to select the read mode for my camera you must go into this code and change the value "4" to the desired mode from "0" to "4".(each number setting a different read mode) To make life easier I made a popup menu in my GUI that lets you pick which readmode is activated.
This is the code for the popup menu:
% --- Executes on selection change in Read_mode.
function Read_mode_Callback(hObject, eventdata, handles)
% hObject handle to Read_mode (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 Read_mode contents as cell array
% contents{get(hObject,'Value')} returns selected item from Read_mode
FVB=0;
Multi_Track=1;
Random_Track=2;
Single_Track=3;
Image=4;
items = get(hObject,'String');
index_selected = get(hObject,'Value');
item_selected = items{index_selected};
if item_selected==0;
SetReadMode(0);
end
if item_selected==1;
SetReadMode(1);
end
if item_selected==2;
SetReadMode(2);
end
if item_selected==3;
SetReadMode(3)
end
if item_selected==4;
SetReadMode(4)
end
How can I replace "setreadmode(4)" in my "previmage" button with a line of code that takes whatever has been selected from this popup menu instead?
Thanks

 Accepted Answer

get( handles.Read_mode, 'Value' )
should give you the index of the currently selected option in your popup list.

11 Comments

Hi Adam thanks for the help, so do I just replace setreadmode() with this in my code for the "previmage" button? Im not 100% familiar with matlab you see... When I do this does it look for the setreadmode button and use whatever has been selected from the list?
So I replaced "setreadmode()" with:
get( handles.Read_mode, 'Value' )
SetReadMode(item_selected);
CheckWarning(ret);
Does this seem right? Although when I click on the "previmage" button I get this error in my main matlab window:
Starting Acquisition
Warning: Andor SDK Returned Error: DRV_ACQUIRING
[20072]
> In CheckWarning at 129
In CamTestGUIbackup>previmage_Callback at 1539
In gui_mainfcn at 96
In CamTestGUIbackup at 42
In @(hObject,eventdata)CamTestGUIbackup('previmage_Callback',hObject,eventdata,guidata(hObject))
In CamTestGUIbackup>previmage_Callback at 1568
In gui_mainfcn at 96
In CamTestGUIbackup at 42
In @(hObject,eventdata)CamTestGUIbackup('previmage_Callback',hObject,eventdata,guidata(hObject))
In CamTestGUIbackup>previmage_Callback at 1568
In gui_mainfcn at 96
In CamTestGUIbackup at 42
In @(hObject,eventdata)CamTestGUIbackup('previmage_Callback',hObject,eventdata,guidata(hObject))
The image is still shown anyway so I'm not too sure what it is even affecting
I am assuming that your setreadmode function take an integer as its input and that those inputs are ordered in the same way as in your popup list?
In that case then yes you can just plug the value from my above command straight in as the argument to your setreadmode() function.
If you actually need the string for the selected item then you would also need to get the string from the popup and index into that e.g.
popupStr = get( handles.Read_mode, 'String' );
selection = popupStr{ get( handles.Read_mode, 'Value' ) };
Do you mean you put the following in your push button callback?
item_selected = get( handles.Read_mode, 'Value' )
SetReadMode(item_selected);
otherwise the value you retrieved from the popup was lost and I'm not sure what item_selected refers to.
Do you need the read mode to update when you change the popup selection or only when you press your push button?
If the latter then you don't need a callback at all for your popup - just set it up initially with the options you want in a cell array of strings. Then use the method described above in your push button callback to retrieve the selected option.
Yea that's what I have in now, I need it to update whenever I change it in the popup selection as there are other buttons that will start the camera. But for now I have tried this to see if I can get it going, just to update when I press the push button but I get this error:
Initialising Camera
Configuring Acquisition
item_selected =
5
Starting Acquisition
Error while evaluating uicontrol Callback
Its odd that item_selected is 5, whenever my read modes go from 0 to 4. Maybe it is getting mixed up here as the readmode I picked was mode 4 which is the 5th number?
Sorry did you mean use:
popupStr = get( handles.Read_mode, 'String' );
selection = popupStr{ get( handles.Read_mode, 'Value' ) };
As if I replace "setreadmode()" with this I still get an error
>> CamTestGUIbackup
iCam Live Mode Preview
Error while evaluating uicontrol Callback
Ah, sorry, indexing into a popup list starts from 1 so if your function expects a 0-indexed answer you will have to subtract 1 from the value returned from the popup.
Ok thanks for the help,
get( handles.Read_mode, 'Value' );
SetReadMode('Value'-1);
That's it there right?
value = get( handles.Read_mode, 'Value' );
SetReadMode( value - 1 );
Cheers, one more question... Should I not define what each of the values in the popup menu are qual to?
ie: FVB =1 Multi-Track=2 etc
Or does matlab just automatically assume that FVB is equal to the value of 1?
Its just that if I select any other readmode matlab runs into a loop and doesn't display an image at all, although this is more than likely due to some other error on my part
Your SetReadMode function presumably has the interpretation of the raw values as FVB or Multi Track or whatever.
In the popup list that interpretation comes from the order in which you put the string of options together for the popup list. This must be the same as the order you interpret them as in your function.
The loop issue sounds like a different problem though.

Sign in to comment.

More Answers (0)

Asked:

on 15 Sep 2014

Commented:

on 16 Sep 2014

Community Treasure Hunt

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

Start Hunting!