Matlab GUI popupmenu problem

Hello;
I used popupmenu in Matlab GUI why it refuse to write in the output box

回答 (1 件)

Geoff Hayes
Geoff Hayes 2022 年 3 月 15 日

0 投票

@Huda M - the red text in your screen shot is the error message so it is telling you what is going wrong. It would be helpful to copy and paste the full error message to this question so that anyone can get an idea of what is happening. Looking at your code though (again, copying and pasting the code is more helpful than a screen shot)
set(handles.output, 'string', 1)
it seems that you are setting the output text field string property with an integer. Try doiing
set(handles.output, 'string', '1')
instead where you set the string property with a string. Please do that for each of the four conditions.

5 件のコメント

Huda M
Huda M 2022 年 3 月 15 日
I did this
set(handles.output, 'string', '1')
but it did not work
Here is the code:
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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 popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
Val=get(handles.popupmenu1,'value')
if (Val==1)
set(handles.output,'string','1')
elseif (Val==2)
set(handles.output,'string','2')
elseif (Val==3)
set(handles.output,'string','3')
else
set(handles.output,'string','4')
end
Huda M
Huda M 2022 年 3 月 15 日
the code is simple I don't know why it does not work
Geoff Hayes
Geoff Hayes 2022 年 3 月 15 日
@Huda M - can you please copy and paste the full error message to this question? Your second screen shot doesn't show what the error is (only the last few lines of it). I suspect that the output object/control is not a text control and so doesn't have the String property. But this is just a guess...
Huda M
Huda M 2022 年 3 月 16 日
Error using matlab.ui.Figure/set
Unrecognized property string for class Figure.
Error in GUI4>popupmenu1_Callback (line 81)
set(handles.output,'string',val);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI4 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI4('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Geoff Hayes
Geoff Hayes 2022 年 3 月 16 日
@Huda M - I think the problem is the name of your text (edit) control. It can't be named output as that conflicts with a default line of code in the OpeningFcn of your GUI. If I create a GUIDE GUI, I see the following
% --- Executes just before untitled2 is made visible.
function untitled2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled2 (see VARARGIN)
% Choose default command line output for untitled2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
Note the line
handles.output = hObject;
which overrides your text control with the handle to the figure. And so the error message Unrecognized property string for class Figure makes sense. Just rename your text control to something else and your GUI should work fine.

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

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

タグ

質問済み:

2022 年 3 月 15 日

コメント済み:

2022 年 3 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by