Extracting selected string from popupmenu

Hi,
I have created a popupmenu tag named popup1 and using uicontrol which has table name from my database as its String content.
function example1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
con=sqlite('db123');
data=fetch(con,'SELECT name FROM sqlite_master WHERE type=''table'' AND name<>''sqlite_sequence''');
[r,c]=size(data);
for n=1:r
datstr(n)=data(n,:);
end
datstr=string(datstr);
uicontrol('Tag','popup1','Style', 'popupmenu','Position',[30 353.2 130 25],'String',{' ' datstr});
when I select a table name the name should be displayed in a text box This is what I have done inside popup1_callback function
function popup1_Callback(hObject, eventdata, handles)
strv=get(handles.popup1,'String');
set(handles.longedit,'string',strv);
How can I achieve solution to my problem.
Thanks in advance

Antworten (2)

Jan
Jan am 21 Aug. 2017

1 Stimme

Perhaps you mean:
strv = get(handles.popup1, 'String');
value = get(handles.popup1, 'Value');
set(handles.longedit, 'string', strv{value});

1 Kommentar

nikhil nambiar
nikhil nambiar am 22 Aug. 2017
I tried your solution this is giving me an error error:Function 'subsindex' is not defined for values of class 'cell'

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 22 Aug. 2017

0 Stimmen

You have
function popup1_Callback(hObject, eventdata, handles)
which almost always means that GUIDE believes that you already created a uicontrol with tag popup1 and saved it with the .fig
But then you have
function example1_OpeningFcn(hObject, eventdata, handles, varargin)
...
uicontrol('Tag','popup1','Style', 'popupmenu','Position',[30 353.2 130 25],'String',{' ' datstr});
so it appears that you are creating popup1 in that callback. I suspect that is creating a second uicontrol with tag 'popup1'
If I am correct, then under some circumstances GUIDE would set handles.popup1 to a vector containing both handles. If that were to occur, then
value = get(handles.popup1, 'Value');
would retrieve the Value for both handles and put the result into a cell array. It would then be illegal to try to index by that resulting cell array.

5 Kommentare

nikhil nambiar
nikhil nambiar am 22 Aug. 2017
I had created popup callback and createfcn function manually. and popup1 is only visible when example_open1 function is triggered
Time to use the debugger then. At the command line command
dbstop if error
and run the code. When it stops, look at the variable value and check to be sure that handles.popup1 is a scalar.
nikhil nambiar
nikhil nambiar am 22 Aug. 2017
Bearbeitet: nikhil nambiar am 22 Aug. 2017
No error. when a choice is selected no event occurs. Can you guide me how to call popupmenu in opening function and update changes through its callback function
Walter Roberson
Walter Roberson am 22 Aug. 2017
However just before this, you were getting Function 'subsindex' is not defined for values of class 'cell', and with that version of the code, you should be able to use "dbstop if error"
Image Analyst
Image Analyst am 22 Aug. 2017
Time to quit guessing. Attach your .fig and .m files with the paper clip icon so we can solve this finally.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Debugging and Analysis finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 21 Aug. 2017

Kommentiert:

am 22 Aug. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by