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

조회 수: 2 (최근 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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by