Error in For loop

4 views (last 30 days)
Chethan
Chethan on 14 Apr 2013
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?

Answers (1)

Image Analyst
Image Analyst on 14 Apr 2013
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 Comments
Chethan
Chethan on 14 Apr 2013
Well, sorry where i need to assign and what i need to assign?
Image Analyst
Image Analyst on 14 Apr 2013
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.

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!