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 view (last 30 days)
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 Comments
Rik
Rik on 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 on 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

Sign in to comment.

Accepted Answer

Greg
Greg on 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 Comments
Walter Roberson
Walter Roberson on 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

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!