I have been working on a project I should do in MATLAB Appdesigner. What I am trying to do is when the Switch button is pressed, the value of the textbox changes to my desired value.(This just only a bit of the project, not all of it). For instance, as illustrated below, if the user clicks on the switch button to set it on Transient mode, the value in the textbox automatically changes to a specific number.
You have to add a callback to your switch button. Assuming the text field is called EditField and the switch is called Switch, this call back will change the EditField value to 10 every time you change it to Transient:
% Value changed function: Switch
function SwitchValueChanged(app, event)
value = app.Switch.Value;
if strcmp(value, 'Transient')
app.EditField.Value = '10';
end
end
Note that the value of the switch is whatever you wrote on the interface, so if you change Transient or Steady to something else, you will have to change your code. Also note that EditField.Value expects a char array.
Related
Basically I have a really complex Matlab GUI, where the user can add different strings by an editextbox (the one you can type in). The problem is that I assigned different functions to different keyboard button pushdowns, for example 's' and 'e' are in use. Every time you try to type in for example 'snake' the functions assigned to 's' and 'e' are executed. Is there any way to determine wheter I clicked in the edittextbox, and it is in use -> like a logical value so i can block the keypressfunctions by checking it.
Within your callback, you can use gco to get the current object. You can then check if this is the graphics handle to your edit box.
fig = figure('WindowKeyPressFcn', #(s,e)keypress());
hedit = uicontrol('Style', 'edit');
function keypress()
if ~isequal(gco, hedit)
disp('Window Key Press')
end
end
If on the other hand you didn't set a global key press callback (using WindowKeyPressFcn) and you instead set the KeyPressFcn of each uicontrol individually, just specifying a different (or no) KeyPressFcn for the edit box would work.
I have four items(Types of soils) to be inserted in popup menu and the output should be integer values(porosities) displayed using a static text. How this can be done?
I guess you will be using GUIDE to create your GUI. Here is a simple code for populating a popup menu with pre-defined strings and use the selected item to change the displayed output in the static text:
1) In order to populate the popup menu, simply create a cell array of strings and set the 'String' property of the menu to that cell array:
% Create cell array
SoilTypes = {'Soil A' 'Soil B' 'Soil C' 'Soil D'};
% Set the string property of the popup menu:
set(handles.popupenu1,'String',SoilTypes);
2) Now the callback you are talking about seems to be the Selection Change callback, which might look like this:
function Popupmenu1_SelectionChangeFcn(hObject, eventdata, handles)
This gets executed when the user changes the selected item in the popupmenu. Therefore, you want to get the selected item with this line:
get(handles.popupmenu1,'Value')
So according the the previous cell array, you can use (among other options) a switch/case scenario in which you can do whatever stuff you want for every type of soil, such as displaying text in the static text box. You can also set directly the string in the text box like so:
SelectedItem = get(handles.popupmenu1,'Value') % Get the value, from 1 to 4 in your case
set(handles.text1,'String',SoilTypes{SelectedItem}); % Display the corresponding soil type
Hope that helps get you started!
I have two radio buttons in my interface. I also have some pushbuttons. When ever I click a pushbutton I want it to call a function according to the selected radio button.
I tried adding the function given below
function rotation_SelectionChangeFcn(hObject, eventdata, handles)]
Tag = get(hObject, 'Tag');
disp(Tag);
But nothing is coming up when I change the selection. I want to know whether there is any mistake in the way I implemented the code or is there a better way of doing this?
Whenever you use radiobuttons you might want to regroup them in button groups, then it's quite easy to play with radio buttons, and you make sure that only 1 radio button per group gets selected at any time.
For radio buttons in button groups, you want to use the following:
get(eventdata.NewValue,'Tag')
to get the tag of the new value just being selected. You can also use OldValue as well if you want.
In the callback of your pushbutton, you can query whether a radio button is activated with its 'value', i.e. 1 if it is selected.
StateRadioButton = get(handles.RadioButton1,'Value'); %assuming the tag is "RadioButton1".
The hObject property is particular for the specific callback in which it is used, otherwise you need to use the handles structure to access elements from other functions.
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.
I've a pop-up menu with 5,10,15,20 the contents in that menu. using switch I've created this
val=get(hobject,'value');
switch val
case '5'
n=5;
case '10'
n=10;
case '15'
n=15;
case '20'
n=20;
end
guidata(hObject, handles);
where it represents number of output images. On pressing search button in the same GUI window it calls another function where i need to use this 'n'.
for i = 1:n % Store top n matches...
tempstr = char(resultNames(index(i)));
fprintf(fid, '%s\r', tempstr);
disp(resultNames(index(i)));
disp(sortedValues(i));
disp(' ')
end
How can i pass this 'n' to that code or function?
any proper answer is appreciable.
Well, to start with your switch statement is incorrect and unnecessary. The Value property of a dropdown is not the text contained in the current selection, it is the index of the current selection in its list. To get the string value of the list item currently selected, you would do:
contents = cellstr(get(hObject,'String')) % returns contents as cell array
contents{get(hObject,'Value')} % returns value of selected item from dropdown
That is, of course, assuming hObject is a handle that points to your dropdown box - which it will be only if you're in a callback that was raised by the dropdown itself. Further to that, note that there is no need to convert the string value via a discretised switch statement; you can just use the str2num or str2double functions.
Finally, if you need to access the value of the dropdown from outside one of its own callbacks, you need to use the handles structure that is passed into every callback (or that, in your sample, is returned from guidata). There will be a field in handles with the same name as your dropdown - this will be the handle via which you can access its properties.
The way to pass information around a GUI is to use the the handles structure. If you created your GUI using GUIDE handles should have been created in the opening function. You can modify the opening function to add field and initial values to handles. For example, you could add the following to the opening function:
handles.n = 1; % This initializes handles.n to a default value in case the search button is
% pushed before an item in the menu is selected.
Then include the following in the call back for the menu to update and store the value of n:
handles.n = val; % This is updated every time an item from the menu is selected.
guidata(hObject,handles);
In the call back from the search button you can access the value of n and pass it to your other function like this:
n = handles.n;
myFunction(n);
Your other function will have start with something like this:
function [] = myFunction(n)
followed by the rest of the code you included above. You'll have to make sure myFunction.m is in the Matlab search path (can be set using addpath or by clicking the set path button in Matlab.)