Simple gui programing with pop-up manu.
이전 댓글 표시
Hello. I have the following simple GUI design.
The upper text box (which tag is input) is where the input number is entered while the lower text box (tag is output) presents the place where the output value appears.
The center pop-up menu (tag is select) has three calculation option with the input number; square, cubic and exponential. The output value calculated according to the choice of pop-up menu should be shown in the lower text box.

The corresponding m file is the following.
% --- Executes on selection change in select.
function select_Callback(hObject, eventdata, handles)
% hObject handle to select (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns select contents as cell array
% contents{get(hObject,'Value')} returns selected item from select
n = str2double(get(handles.input,'String')); % Obtaining number entered on the upper text box.
choice = get(hObject,'Value'); % Obtaining the selection from pop-up menu.
switch choice
case 1
y = n^2;
case 2
y = n^3;
case 3
y = exp(n);
end
set(handles.output,'string',y); % Print the output value at the lower text box
I didn't touch anything on the other part of the code.
I guessed there is no defect in this code unless I saw the error message like
Error using hg.figure/set
The name 'string' is not an accessible property for an instance of class
'figure'.
Error in practise_2>select_Callback (line 117)
set(handles.output,'string',y);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in practise_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)practise_2('select_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
after the code running. Please tell me anything wrong on my code
In addition, could you tell me how I understand the command 'set'.
How is set used to give the output to the lower textbox?
채택된 답변
추가 답변 (1개)
Image Analyst
2014년 2월 25일
Looks like y is a number so if you want to send a string to the edit box, you should use num2str:
set(handles.output,'string', num2str(y));
or you can probably do
set(handles.output,'Value',y);
Also, usually GUIDE creates output automatically so I never use it. Maybe there's some conflict. You might try calling it output1 or something instead.
댓글 수: 2
Dong-Gyu Jang
2014년 2월 25일
Image Analyst
2014년 2월 25일
If you attach your m-file and fig-file, I might have time to look at it tomorrow.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!