Weird behavior of Popup Menu while dynamically populating data

I am fairly new to matlab and trying to implement a feature where I can populate data in table based on popup menu selection. Here is sample code
for chan = 1:n
if (chan > 1)
channels = channels + newline + "Ch. " + chan;
else
channels = "Ch. " + chan;
end
end
set(handles.popupMenu,'String', channels,'Value', 1);
handles.popupMenu = Popup Menu
n = 3
It does work good. It outputs 3 Options in Popup Menu, Channel 1 , Channel 2 and Channel 3. Firstly How can I set the values too? Because when I use the same array it gives me error no Value property found
Secondly on changing the value of popup menu
% --- Executes on selection change in coloredChannel.
function coloredChannel_Callback(hObject, eventdata, handles)
contents = get(hObject,'String');
fprintf('%s\n', contents)
Returns
CCChhhaaannnnnneeelll 123
as response.
What is the above response and how can I get the selected item value or string.

Réponses (1)

When you set the String property of a uicontrol with a scalar string object or a character row vector, then the content is analyzed for | characters and newlines to determine whether multiple rows should be created. The content is converted to a char array with the appropriate number of rows, and that is what is stored in the String property. Then when you retrieve the String property, the usual rules apply for fprintf() of arrays: namely that values are displayed down columns first (that is, linear indexing is used.)
I suggest
contents = cellstr( get(hObject,'String') );
fprintf('%s\n', contents{:})

2 commentaires

Thanks for your kind reply. However how can i set values and get the "current" selected item?
chan = get(hObject, 'Value'); %the number of the channel
chan_entry = contents{chan}; %the string corresponding to the channel

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by