I want to give list of value in popup menu and use that in push button.

6 views (last 30 days)
Here is my code.
function SEC_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Not sure if handles is up to date already
switch get(handles.SEC, 'value')
case 1
S.a=7;
S.b=20;
case 2
S.a=5;
S.b=30;
otherwise
end
set(handles.popupmenu, 'UserData', S);
function ANS_Callback(hObject, eventdata, handles)
S = get(handles.SEC, 'UserData');
a = S.a;
b = S.b;
c = a + b;
set(handles.AANS,'string',num2str(c));
I get error 'Dot indexing is not supported for variables of this type.'
Error in pop>ANS_Callback (line 113)
a = S.a;'
Please help me.
pop.PNG
  7 Comments
Yash Vaid
Yash Vaid on 17 Jan 2019
Thank you very much sir for your continues support. But I excatly cant understand what should I do. Please either send the code and procedure with figure file or decribe in simple way because I am beginner in GUI. Again thank you sir but please help me to fix this problem.
Yash Vaid
Yash Vaid on 17 Jan 2019
When I change popup menu option it gives error
'SWITCH expression must be a scalar or a character vector.
switch domaintype '
and when I push answer buttton it gives error
'Reference to non-existent field 'a'.
a = handles.a; '

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Jan 2019
Note that when you initialize the pop-up by loading the figure constructed in GUIDE (or by creating a uicontrol), that the default Value property is empty -- and even if the Value happened to be set to something else at the time you had saved the figure in GUIDE, then the Callback property associated withe the control is not run when the control is loaded or created.
Thus, at the beginning, even though 'first' might be showing, the action associated with clicking 'first' has not been run. Furthermore, when you click on a popup value that is already the one selected, the callback might not be run and you might have to select a different value and then click back on the original in order to have the value associated with the original be set.
Therefore you need to be careful about how you initialize the controls.
One approach is to have your OpenFcn callback for the GUI set() the Value property of the control, and then execute the callback associated with the control in order to have the sequence run as-if that value had been manually clicked.
If you take this approach, be careful about what you invoke. The callback property that GUIDE stores is not a direct reference to the function to be called: instead it has to retrieve the handles structure and pass that into the function. Historically that involved executing a character vector, but that was improved later, but you might have to fake the callback object to get it to work. More secure is to retrieve the handles structure yourself and call the function yourself.
  2 Comments
Walter Roberson
Walter Roberson on 17 Jan 2019
function SEC_Callback(hObject, eventdata, handles)
listStrings = get(handles.SEC,'String');
idx = get(handles.SEC,'Value');
if ~isscalar(idx)
waitfor( warndlg('No SEC entry has been selected yet') );
return
end
domaintype = listStrings{idx};
switch domaintype
case 'first'
a=7;
b=20;
case 'second'
a=5;
b=30;
otherwise
waitfor( warndlg('Select first or second') );
return
end
handles.a = a;
handles.b = b;
guidata(hObject, handles);
function ANS_Callback(hObject, eventdata, handles)
set(handles.ANS,'UserData',0);
if ~isfield(handles, 'a')
waitfor( warndlg('You need to select SEC first') );
return
end
a = handles.a;
b = handles.b;
c = a + b;
set(handles.AANS,'string',num2str(c));

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!