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
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?
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!