I'm using MATLAB GUIDE to build a GUI to control a piece of hardware. I have a data table the user needs to fill, and every time they select a cell a new configuration is sent to the hardware. This is easily done using the CellSelectionCallback().
However, I also have a single reference value that is currently being entered into an edit textbox. I would like for the GUI to send a new configuration to the hardware whenever a user selects the textbox, but before they enter their new value. Is there a similar SelectionCallback function that can be used for the textbox? The best solution I've come up with is to create a 1x1 table instead of an edit box.
Your best bet is going to be to use a Java control since the standard MATLAB uicontrol does not have a focus callback. You can use a JTextField object, add it to your GUI with javacomponent and then register the FocusGainedCallback.
jtext = javax.swing.JTextField();
jtext.setHorizontalAlignment(javax.swing.JTextField.CENTER);
% Add the control to your GUI
htext = javacomponent(jtext, [10 10 200 10]);
% Register a callback to be executed when the box is selected
set(htext, 'FocusGainedCallback', #yourCallback)
Related
I am using a Matlab based program that does some nice plots of some model results. It adds uicontrol slides and buttons in figures. I have no expierence with gui programming in Matlab, and I dont need it, I just wanna add on my matlab script a couple of lines to hide slides and buttons. I can do it manually from the property editor and set "Visible" to "off", but I was reading the Matlab manual and it does not explain how to retrieve an existing uicontrol and change its properties. Any hint? I tried this with no luck:
b = get(gcf,'uicontrol');
set(b,'Style','pushbutton','Visible','off');
Thanks
You simply need to access the element from the handles structure and change its property from there.
For example, if the pushbutton is stored in the handles structure like this:
handles.b %// Whatever name you gave it and see in the Property Inspector
you can make it not visible using the command
set(handles.b,'Visible','off')
and likewise for every other properties.
Little trick: If you need to repeatedly turn on and off elements of your GUI, you can put them in an array of handles for example in the Opening_Fcn of the GUI and change them all at once using this array anywhere in the GUI. This way you won't have to always call them one by one which can be tedious.
Example:
handles.AllButtons = [handles.button1; handles.button2; handles.button3]
this contains the handles to 3 pushbuttons let's say. Now if yu need to turn them all off/on at the same time, you can do:
set(handles.AllButtons,'Visible','off')
instead of doing
set(handles.button1,'Visible','off')
set(handles.button2,'Visible','off')
set(handles.button3,'Visible','off')
From GUIDE, you can check the actual name of any uicontrol component in the Property Inspector. Here is a screenshot from a GUI I made with GUIDE:
In this case, the Tag associated with the button is pushbutton28_ReferenceChannelApply.
Therefore, in order to change any of its properties I would need to use:
set(handles.pushbutton28_ReferenceChannelApply,'Property','value')
EDIT 2
You can look for pushbuttons in your GUI with the findobj command like so:
FindButtons = findobj('Style','push')
which will output an array of handles to those pushbuttons. Then you can query their properties using the get command:
get(FindButtons(1))
I have two GUI's, 1 main and the other the sub GUI. I want to have the result from the main gui to be displayed in the text box in the sub GUI.
I connected my main gui to the sub gui by adding this:
openfig subgui.fig
I know that this wont do, I'm new in matlab. To display the result in the main gui i have:
set(handles.edit1,'String',f);
f represents the result that i want to be displayed in the other gui.
You need a global variable.
Let’s say that we have
a main GUI named main.m (with an associated main.fig) with one editbox (main_edit) and one pushbutton
a sub GUI named sub.m (with an associated sub.fig) with one editbox (sub_edit) that will get the value form the editbox in main
In main.m
Step 1. Inside the editbox’s callback, add the following:
global my_data;
my_data.main.main_edit = get(hObject, ‘String’);
Step 2. Inside the pushbutton’s callback, add the following just before it’s return:
global my_data;
sub;
delete(handles.main_figure);
In sub.m, inside the opening function, sub_OpneningFcn, add the following:
global my_data;
set(handles.sub_edit, ‘String’, my_data.main.main_edit);
Let me know if it works for you!
Also, there are some awesome videos that you can check released by MathWorks Engineers, here is one video that can help you: http://www.mathworks.com/matlabcentral/fileexchange/8616-video--guide-advanced-techniques
Reverie - the line of code
openfig subgui.fig
will just open the GUI/figure and NOT launch the GUI in a way that can it can be used. While opening it will display the GUI with all of its controls, you will get errors (to the effect of Attempt to reference field of non-structure array.) as soon as you try to use it. Instead, launch the GUI by name as
subgui
which will be sufficient to start the sub-gui in a working manner.
Now, in order to pass information from one GUI to the other, you can try the following. Assuming that you are using GUIDE to create your main and sub GUIs (which seems valid since you have a figure for the GUI), open the property inspector for the figure of each GUI and set the HandleVisibility property to on. At the same time, assign a Tag for each, perhaps MainGui for the main GUI, and SubGui for the sub GUI.
Now, you can use the findobj function to find the other GUI using its tag. Suppose then, that we launch the sub GUI from a pushbutton callback of the first GUI like
function pushbutton1_Callback(hObject, eventdata, handles)
% launch the sub GUI
% NOTE - you may want code here to check to see if the GUI is already open before
% launching it again
subgui;
% find the handle to the subgui
hSubGui = findobj('Tag','SubGui');
if ~isempty(hSubGui)
% get the handles structure of the sub GUI
hSubGuiHandles = guidata(hSubGui);
% get the data from the main GUI to pass to the sub GUI
value = get(handles.edit1,'String');
% now update an equivalent edit text field in the other GUI
set(hSubGuiHandles.edit1,'String',value);
end
In the above we use findobj to find the GUI we are interested in using its Tag property. If we have found this GUI (and so hSubGui is non-empty) then we get its handles structure so that we can update its edit text field with the data in the main GUI.
I have a GUI with an edit box and a push button on the tool bar (well, more things than that, but those are the only things that matter!) Anyway, I have it so when you press the push button tool a variable is set to equal the string in the edit box. A simple var = get(handles.edit1, 'string'). However, when I go straight from entering the value in the box to clicking the pushbutton (without clicking anywhere else or pressing return), var is assigned the previous value in the edit box. Why is this? Is there any way to make sure the push button tool will pick out the correct value?
The GUI was made using guide, if that matters.
This is happening because the uipushtool callback is executing before the text box has time to 'validate'. There is surely a more elegant way to do that but this trick works:
You can use the waitfor command to tell the uipushtool callback to wait for the editable box to validate its input. Unfortunately is is not exactly enough so we'll have to:
1) pass the focus to a dummy control (I created a dummy pushbutton named pushbutton1 which does nothing. The focus could be send to any other dummy control.
2) now wait for the text box to validate its content.
3) When the text box is done, then retrieve the content the traditional way.
This method requires:
- A dummy control to send the focus to (but you can use a real one too)
- An dummy (or not) callback function for your editable box. (the waitfor instruction will wait for the callback to finish, if there is no callback, it will error
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
uicontrol( handles.pushbutton1 ) ; %// pass the focus to a dummy control
waitfor(handles.edit1,'String'); %// wait for the editable box to validate its content
var = get(handles.edit1, 'string') ; %// now retrieve the editable box content
set( handles.text1 , 'String' , var ) %// this can be deleted, just to verify the method
% --------------------------------------------------------------------
function edit1_Callback(hObject, eventdata, handles)
%// This is the dummy callback function for the editable box
%// Do absolutely nothing here (or do if you want ... your choice)
EDIT
as I feared, my initial solution was too dirty to be robust enough. With 2 (or more) textboxes, I tried a large number of things but without success. I dropped the waitfor, I noticed that in some case, when the uipushtool was pressed, the callback of the editbox would fire but the uipushtool would not execute at all ... so i tried to managed things directly from the editbox callback => from the editbox keypressedFcn send each character as we type to a variable ... but even this editbox callback doesn't know the content of the edit box at the time it is executed ?? (This link provide a very simple example to reproduce that).
So to my great own disappointment, I had to resort to an 'external' solution. (If you accept that it becomes very simple though). The trick is to retrieve the handle of the Java EditBox object. Once we have the handle, getting the "real time" content is just matter of converting the Java string into a Matlab string.
To retrieve the Java object handles, you need the function FindJObj from Matlab central.
Put that anywhere in your matlab path, then the code for your pushbutton become something like :
To make sure there was no interferences between the text boxes and the uipushtools I made 2 separate uipushtools each controlling 1 editbox
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
jEditbox = findjobj(handles.edit1); %// get the handle of the java editbox #1
var = char(jEditbox.getText) ; %// retrieve Java string and convert it to matlab string
disp(['uipushtool1_ClickedCallback running. Textbox1 content = ' var ]) %// debug line, you can delete or comment that
% --------------------------------------------------------------------
function uipushtool2_ClickedCallback(hObject, eventdata, handles)
jEditbox = findjobj(handles.edit2); %// get the handle of the java editbox #2
var = char(jEditbox.getText) ; %// retrieve Java string and convert it to matlab string
disp(['uipushtool2_ClickedCallback running. Textbox2 content = ' var ]) %// debug line, you can delete or comment that
Thanks to Yair Altman for the findjobj function, and many other contributions for Matlab users.
I tried to replicate your problem and created a new GUI using guide with only a pushbutton and an edit field. If I set the button callback to:
function pushbutton1_Callback(hObject, eventdata, handles)
get(handles.edit1,'string')
it always prints the current value of the edit field, no matter if I click somewhere else after entering the value or not. Which version of Matlab are you using? I've tested it using Matlab 2010a.
I'm having a bit of trouble with Matlab's uicontrol handling. Here's the situation:
My (programmatic) GUI contains an editable text field. This field originally contains a numeric value and is supposed to always contain one. In order to ensure that, the 'String' parameter is to be repeatedly checked, converted to numeric, and re-entered into the 'String' property of the uicontrol. This is supposed to happen whenever a user enters a visible character into the text field.
Additionally, the program is supposed to notice at any point whether the current value of said text field differs from the original value, which is stored in memory. If the user closes the figure, a modal dialog opens and asks if he wants to save the change to that value or discard it. If the figure is then reopened (through the rest of the GUI), the value of the text field starts out as either the same it started out as before (if the user discarded the changes) or the changed value defined by the user.
Both of these things fail in some instances because of a strange behaviour: The uicontrol's 'String' property is only updated after the uicontrol loses focus. This is not a problem if the user wants to do other things on that GUI or knows how to handle this. I can also live with my text field only updating to a meaningful numeric value after it loses focus.
The problem is, when I close the figure via Windows' red X button in the top right corner after changing the value of the text field, but without first clicking somewhere else, the 'String' value is never updated, so the CloseRequestFcn doesn't notice the change and simply closes the window. Even pausing for a whopping 100ms and then explicitly calling the text field's Callback from within the CloseRequestFcn doesn't help, it just closes without my dialog. The reverse is also true, if I do change the value, click somewhere else, and then change it back, it will display the dialog unless I first click somewhere else again.
So the question is this: How do I ensure the uicontrol correctly updates/executes its Callback when I click the red X button provided by Windows?
And bonus question: How do I execute this Callback "on the fly" in order to correct any impossible values the user enters as soon as he does (e.g. letters other than e and i)?
Here's the isolated part of my GUI that causes the problem, copy into a file called 'guitest.m' and run in order to test what I'm talking about.
function guitest
changed = false; %// tracks changes to catch closing without saving
startval = 1; %// the starting value
handles.figure = figure('Resize','off','Toolbar','none','Menu',...
'none','Name','Change text field value','CloseRequestFcn',...
#closefig,'Visible','off'); %// not visible until fully created
handles.textfield = uicontrol('Style','edit','Units','normalized',...
'Parent',handles.figure,'String',startval,...
'Position',[.4 .6 .2 .1],'Callback',#changedValue);
handles.button = uicontrol('String','Push me','Units','normalized',...
'Parent',handles.figure,'Position',[.4 .3 .2 .2],'Callback',#dispval);
%// all done, display GUI
set(handles.figure,'Visible','on');
%// callback functions
function closefig(~,~) %// Quit program
pause(0.1);
changedValue(handles.textfield); %// update fields
if changed
disp('Do you really want to close?'); %// save dialog
else
delete(handles.figure); %// this is used as CloseRequestFcn!
end
end
function changedValue(hObject,~)
newval = uint16(str2double(get(hObject,'String')));
changed = (startval ~= newval);
end
function dispval(~,~)
disp('With buttons inside the GUI, it works properly:');
disp(get(handles.textfield,'String'));
disp(changed);
end
end
Will forcing a GUI focus change with uicontrol in your closing function provide the correct behavior? From the documentation:
uicontrol(uich) gives focus to the uicontrol specified by the handle,
uich
For the 'bonus' you're likely going to have to leverage the underlying Java. See the article on editbox data input validation from Undocumented Matlab.
In my (programmatic) Matlab GUI, I have a listbox uicontrol.
What I want is to display checkboxes in front of each option. When a user clicks the checkbox, it's marked (and the element will be considered during the calculations later). While if the user clicks the label, a description of the selected option will be displayed in a text uicontrol to inform the user what the option means.
Basically, I want functionality similar to installation programs where you can select components to install and can get information about said components by clicking them (which does not necessarily mark them as selected).
Is there a way to do this with checkboxes or something similar?
There are actually 2 built-in controls that you could use within Matlab:
com.jidesoft.swing.CheckboxList
com.mathworks.mwswing.checkboxlist.CheckBoxList
Usage example (more details in my Matlab-Java book):
jList = java.util.ArrayList; % any java.util.List will be ok
jList.add(0,'First');
jList.add(1,'Second');
jList.add(2,'Third');
jList.add(3,'and last');
jCBList = com.mathworks.mwswing.checkboxlist.CheckBoxList(jList);
jScrollPane = com.mathworks.mwswing.MJScrollPane(jCBList);
[jhCBList,hContainer] = javacomponent(jScrollPane,[10,10,80,65],gcf);
set(jCBList, 'ValueChangedCallback', #myMatlabCallbackFcn);
jCBModel = jCBList.getCheckModel;
jCBModel.checkAll;
jCBModel.uncheckIndex(1);
jCBModel.uncheckIndex(3);
There's no "ready" way for doing that - as listboxes take only plain strings as entries.
You could "manually" draw checkbox fitted into the area of the listbox, but that might mean quite a lot of work to get everything working...
Another alternative is to go for a java-componenent - e.g. using the jide components available in matlab. See e.g.
http://undocumentedmatlab.com/blog/using-jide-combo-boxes/
for a few examples.