Error in For loop
2 次查看(过去 30 天)
显示 更早的评论
This is a simple code, where based on selection of value in pop-up menu a new figure opens which is decided using for loop.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
val=get(hObject,'value');
switch val
case 1
handles.n=5;
case 2
handles.n=10;
case 3
handles.n=15;
end
%handles.n = val;
guidata(hObject, handles);
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Go.
function Go_Callback(hObject, eventdata, handles)
% hObject handle to Go (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if handles.n==5
Fig5;
elseif handles.n==10
Fig10;
elseif handles.n==15
Fig15;
end
guidata(hObject, handles);
When i run this code, for the first pass I'm getting error like undefined variable handles.n How ever without terminating if i change the value in pop-up menu to 10 and if i press Go button a new Fig10 window will get displayed. Next without terminating if i change the value in pop-up menu to 5 and if i press Go button Fig5 window will get displayed. why I'm getting error for the first time or pass?
0 个评论
回答(1 个)
Image Analyst
2013-4-14
I don't see anywhere in the code that you've posted that handles.n gets assigned, ever. Where did you assign it? You have to assign it before you can use it.
2 个评论
Image Analyst
2013-4-14
In your openingFcn() function set handles.n equal to 5. That way, even if the user never interacts with the popupmenu3, it will still have a value when it gets to the callback of the "Go" button.
另请参阅
类别
在 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!