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 回表示 (過去 30 日間)
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 件のコメント
Rik
Rik 2017 年 9 月 6 日
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 2017 年 9 月 6 日
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

サインインしてコメントする。

採用された回答

Greg
Greg 2017 年 9 月 6 日
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 6 日
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 2017 年 9 月 7 日
Excellent catch. Thanks a lot. It solved my problem..

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by