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

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'));

Related

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!

save data to popupmenu

I have a popup-menu that has an option, add own material, and when this is chosen, a pushbutton is enabled that say SAVE. I alose have a bunch of edit-boxes.
What I want to do is to make a callback so whenever I click the SAVE, the data from one of the textboxes will possible to select in the popupmenu. when this is selected, I want to set the other edit-boxes to contain the same data that they did when I clicked the save button.
Thanks in advance
I'm not quite sure to fully understand what you want, but you can use the handle of the popup menu to get the string/string array in contains at the time you press the pushbutton. Then you can put those inside the textboxes using their handles as well.
For instance:
MyStrings = get(handlesToYourPopupMenu,'String');
or
MyStringsArray = cellstr(get(handlesToYourPopupMenu,'String'));
which contains the content of the popup menu as a cell array.
and
set(handlesToYourEditBox,'String',MyString);
Is that what you mean? If not please ask :)
EDIT:
To add the new data to the existing content of your text box, use concatenation. Since the content is in a cell array, you can do the following:
NewString = [OldString {CurrentString}];
where CurrentString is obtained with
get(hanlesToYourEditBox,'String');
Therefore to update the content of the popup menu you could write this:
set(handlesToPopUpMenu,'String',[MyStringsArray {get(hanlesToYourEditBox,'String')}]):

Updating the selected dropbox item in a GUI

Currently I have a GUI in which once a 'Submit' button is pressed the drop menu which is left blank is then populated by a calculated value determined by the three other values.
I have successfully figured out how to grab all of the values using this logic:
temp=get(handles.FSTOPpopmenu,{'String','Value'});
fstop=temp{1}{temp{2}};
if (strcmp(fstop,'Select'))
fstop = 0;
else
fstop = str2num(fstop);
end
I just have two questions about this that I can not seem to find an answer for.
How would I go about updating the 'empty' drop menu to the calculated variable (the calculated variable will already be one of the possible values in the predetermined list)?
How would I go about presenting an error, say if I have an if statement checking that the amount of zeros in the array? Would a pop up box be sufficient?
Cheers.
As for your first question matlab's set command is what you're looking for. The documentation is here. You would probably need:
MyValueIndex = find(DropDownValues==NewValue);
switch handleToChange
case handles.handle1
set(handles.handle1,'Value',MyValueIndex);
case handles.handle2
set(handles.handle2,'Value',MyValueIndex);
otherwise
error('Uh oh!');
end
Note that MyValueIndex is the index of dropdown box values that you want. Which is found with a find command on the actual value.
Question two is more of an opinion question but I think that a pop-up box describing the problem is sufficient. Maybe add in a system beep for good measure!
Reference:
http://www.mathworks.com/matlabcentral/answers/22734-resetting-a-pop-up-menu-in-a-gui
http://www.mathworks.com/help/matlab/ref/find.html
http://www.mathworks.com/help/matlab/ref/switch.html
For a popupmenu uicontrol the Value property determines which element of the String property is currently being displayed. Get the String; compare its contents to the calculated variable to get the index; then set that index as the Value property. If the calculated variable is not currently in the String then add it to the String and then set the Value. (Note when comparing you need to compare numbers to numbers or string to strings, so you may need to do an appropriate data type conversion first).
Use an errordlg.

MATLAB GUI pop-up menu list get disappear when I set a default string?

can somebody please tell me why my pop-up menu list get disappear when I set a string which actually belongs to one of the item in the list? I want this string to be appear as default when GUI gets open, however, want other items to be in the pop-up menu.
For example, pop-up list contains:
Set_1
Set_2
Set_3
Set_4 etc..
And, in the function OpeningFcn, I am settting:
set(handles.popupmenu1, 'String', 'Set_1');
This makes 'Set_1' to appear when I open GUI. However, it makes other items (Set_2, Set_3 etc) disappear from the GUI. Thanks.
The String property of a popupmenu uicontrol sets the entire text that is displayed in the menu.
To select a particular option, set the Value property to the index of the item to be selected. In this case, since Set_1 is the first item, set the Value property to 1.

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

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);