add/delete numerical values in list box guide of MATLAB using push button and update it using push button - matlab

I need to add/delete numerical values presented in the list box of MATLAB guide after selecting the number of values by the user. Also i need to update the listbox values using push button pressed

Somewhere in your opening function, before the line guidata(hObject, figure1), you should have some variable to keep track of your numbers. Declare something like
handles.numbers = {};
In general, if you have a cell array, cell_array, you can set the listbox like this. In the call back for your push button, first update the cell array and then load it into your list box using this line
set(handles.tag_of_listbox, 'String', handles.numbers, 'Value', 1);

Related

How to get `length` of Matlab GUI popup menu strings

I want to get how many strings exist in the popup menu, how can this be done? This code that I have written does not seem to work.
length(get(handles.popupMenu,'Value'))
Value is the index of the currently selected item in the menu so it will only ever be a scalar. You instead want to check the length of the String property which contains a cell array of strings (one for each item).
nOptions = numel(get(handles.popupMenu, 'String'));

How to write a callback for popup menu in matlab?

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!

Get state of MATLAB GUI radiobuttons?

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.

Matlab returning text on a push button

I am just starting to learn about push buttons and i am stuck. I have two pop up menus. When the user selects the push button. the total from each pop up menu selection is returned to the user. I got the values from the pop up menu but I dont know how to return it after the push button is executed. Any help is appreciated
function pushbutton1_Callback(hObject, eventdata, handles)
math=0;
data1 =get(handles.popupmenu1, 'Value') %processing data from first pop up menu
if data1== 1
math=1
elseif data1 == 2
math=4
end
data2=get(handles.popupmenu2, 'Value') %processing data from second pop up menu
if data2==1
math=math + 5;
end
% I tabulated math which is some number. I want to return it back to the user
in a text outside of the button.
There is another way then returning the value: You pass and store it within the guidata-structure. Search for guidata in the docs for detailed information. One example from the docs:
function My_Callback()
% ...
% Get the structure using guidata in the local function
myhandles = guidata(gcbo);
% Modify the value of your counter
myhandles.numberOfErrors = myhandles.numberOfErrors + 1;
% Save the change you made to the structure
guidata(gcbo,myhandles)
Short explanation how to do it:
1. get the data by myhandles=guidata(handle_of_the_figure)
2. add/modify data, like myhandles.Test = 123
3. dont forget to save the changes, otherwise they will "just disappear"-> use guidata(handle_of_the_figure,myhandles)
4. to test it, just load the guidata afterwards within another function and look for the changes!
EDIT
while re-reading your question, it came to my mind, that you perhaps just want a value calculated within your callback to be displayed somewhere else. for example, if you want the value showing up in an text-edit uicontrol, you can use:
set(HandleOfTheTextEdit, 'String', num2str(mat))
A more elaborate, but more powerful way would be to create your own handle class.
Doing that you could add callbacks to custom data. I did an example here.

GUI pop-up menu in 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.)