problem with nested function in GUIDE

조회 수: 4 (최근 30일)
Ghenji
Ghenji 2018년 5월 2일
편집: Ghenji 2018년 5월 2일
I have got following set of code - It is basically functioning based on the plot data obtained previously. Main function gets all the data and creates a popupmenu. Nested function help me get histogram for each plot individually.
function pushbuttonhistogram_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonhistogram (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filelist;
global subitems_plot;
global subsubitems_plot;
item_index = get(handles.listboxitems, 'Value');
items_list = cellstr(get(handles.listboxitems, 'String'));
item = items_list{item_index};
subitem_index = get(handles.listboxsubitems, 'Value');
subitems_list = cellstr(get(handles.listboxsubitems, 'String'));
subitem = subitems_list{subitem_index};
subsubitem_index = get(handles.listboxsubsubitems, 'Value');
subsubitems_list = cellstr(get(handles.listboxsubsubitems, 'String'));
subsubitem = subsubitems_list{subsubitem_index};
f = figure('Name','Histogram Plot','NumberTitle','off');
figure(f);
len = length(filelist);
ax = axes('Parent',f,'position',[0.13 0.28 0.77 0.54]);
uicontrol('Parent',f,'Style','popupmenu', 'String', filelist, 'Position',[81,54,419,23], 'Callback', @fileselect);
function fileselect(source,event)
val = source.Value;
if isempty(subsubitems_plot)
hist(ax, subitems_plot(:, val));
title(ax, {item;subitem});
else
hist(ax, subsubitems_plot);
title(ax, {item;subitem;subsubitem});
end
end
Problem: Not getting any kind of plot. If I add end after nested functioin am getting error -
Illegal use of reserved keyword "end".
and without 'end' nested function does not connect with main function.
  댓글 수: 2
Ameer Hamza
Ameer Hamza 2018년 5월 2일
Although not an answer to your question, you can create a separate function file and place it in MATLAB path instead of creating nested function. Another solution is to simply place it as a normal function inside m file of the GUIDE. But it depends on your application, whether a nested function is necessary or not.
Stephen23
Stephen23 2018년 5월 2일
@Ghenji: also note that you should use guidata rather than global variables:

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2018년 5월 2일
The required format is clear: The main functions and nested function must be closed by an "end":
function main
function nested1
end
end
function sub1
end
It seems like there is a missing end anywhere in your code outside the posted part. Please control this by an auto-indentation: Ctrl-A Ctrl-I . Does the last line of the file end in the first column?
  댓글 수: 1
Ghenji
Ghenji 2018년 5월 2일
편집: Ghenji 2018년 5월 2일
Thank you Jan. Seems like putting end at right place was the only problem after all. All sorted now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by