I'm trying to build a calculator. I have a listbox and a series of pushbuttons for the operations (+,-,/,sqrt,cos,sin.). I display what the user is clicking on an edit box. When the user clicks on 'cos' e.g., it only displays 'c'. Please check the co

1 vue (au cours des 30 derniers jours)
function calculator(hObject,~,~) %Calculator
handles=guidata(hObject);
h=figure(...));
(...)
uicontrol('Parent',h,'Style','pushbutton','BackgroundColor','w','FontSize',11,...
'FontWeight','bold','ForegroundColor','k','Units','Normalized','Position',[0.88,0.3667,0.0981,0.1],'String','cos','Callback',@expopup);
(...)
handles.calculator=h;
guidata(handles.calculator,handles);
%%%%%%
function expopup (hObject,~,~) %CALCULATOR visior printing;)
handles=guidata(hObject);
ed=findobj(handles.calculator.Children,'style','edit');
if hObject.Value==0
list=hObject.String;
else
list=hObject.String(hObject.Value);
end
txtstr=get(ed,'String');
txtstr=strcat(txtstr,list);
set(ed,'String',txtstr);
  2 commentaires
Rik
Rik le 6 Sep 2017
There isn't an obvious mistake that jumps out to me. Can you make this code into an MWE, which reproduces the problem in as few lines of code you can?
susana
susana le 6 Sep 2017
Hello Rik, Thanks for your quick answer. Yes, please see the attached code. You should be able to run this. Hope you can help me. Best regards, Susana

Connectez-vous pour commenter.

Réponse acceptée

Greg
Greg le 6 Sep 2017
The 'Value' property of a pushbutton uicontrol is set to 1 during callback execution. Therefore, your button vs. list logic:
if hObject.Value==0
list=hObject.String;
else
list=hObject.String(hObject.Value);
end
enters the "else" statement and chooses only the first character of hObject.String. Try looking at a property of hObject that is guaranteed to be listbox-specific ("style" maybe?).
  2 commentaires
Walter Roberson
Walter Roberson le 6 Sep 2017
Good catch.
This section of code looks to me as if it was written with popups or listbox in mind rather than push buttons. For popups and listbox, you would have logic similar to
if isempty(hObject.Value)
list = '?? Nothing Selected ??';
else
list = hObject.String{hObject.Value};
end
But for push buttons, you would just copy the entire String property without indexing
if hObject.Value==0
list = '?? Button is up ??';
else
list = hObject.String;
end
susana
susana le 7 Sep 2017
Excellent catch. Thanks a lot. It solved my problem..

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by