Function fails when called from another function

조회 수: 2 (최근 30일)
Miquel
Miquel 2019년 11월 13일
댓글: Miquel 2019년 11월 14일
The function below fails when I call it from another function. I call it like this:
% Stuff before where all the variables are defined
for nn=7:-1:1
for mm=3:-1:1
baseprofileseason{:,mm}=dayprofile(Days{nn,mm},data{EorG,1},IDs,avehousize(end));
end
end
% Stuff afterwards
The error is this:
Undefined function or variable 'profile'.
Error in dayprofile (line 86)
profile(:,groups+2)=profile(:,groups+1)/avehousize;
Error in projection (line 341)
baseprofileseason{:,mm}=dayprofile(Days{nn,mm},data{EorG,1},IDs,avehousize(end));
And the function is this: (the error appears in the very last line)
function [profile]=dayprofile(days,data,IDs,avehousize)
if ~iscell(IDs)
IDcheck=IDs;
clear IDs
IDs{1}=IDcheck(:);
else
IDcheck=vertcat(IDs{:});
if length(IDcheck)~=length(unique(IDcheck))
error('At least one ID appears to be in more than one group')
end
end
groups=numel(IDs);
indexdays=ismember(data(:,4),days);
for nn=groups:-1:1
indexIDs=ismember(data(:,1),IDs{nn});
focusIDsdays=data(indexdays&indexIDs,:);
periodsnumber=max(focusIDsdays(:,5));
for mm=periodsnumber:-1:1
indexperiod=focusIDsdays(:,5)==mm;
profile(mm,nn)=mean(focusIDsdays(indexperiod,3));
end
end
allIDs=ismember(data(:,1),IDcheck);
allIDsdays=data(indexdays&allIDs,:);
for mm=periodsnumber:-1:1
indexperiod=allIDsdays(:,5)==mm;
profile(mm,groups+1)=mean(allIDsdays(indexperiod,3));
end
% The erromes for the following line
profile(:,groups+2)=profile(:,groups+1)/avehousize(end);
What can I do so that the function works when is called within another function?? I really don't understand why suddently it doesn't find the function at the end when it is using it before.
Find the variables you can use to run this function in the following link: https://we.tl/t-WxrFd3yESm
(one of the variables, 'data', is huge so the .mat file is almost 500MB)
  댓글 수: 2
the cyclist
the cyclist 2019년 11월 13일
편집: the cyclist 2019년 11월 13일
I would recommend using the debugger to figure this out. In particular, you could run
dbstop if error
and then run your code. It will stop at the line where the error occurs, with the workspace "intact". Then you can see the state of affairs just before that line's execution is attempted.
As Walter references in his answer, a likely culprit is that the preceding for loop is never entered.
After you are done debugging, you can type
dbclear if error
to exit the mode in which code will automatically stop for errors.
Miquel
Miquel 2019년 11월 14일
Oh, that is useful! Now I guess there are lots of functionalities of the debugger which I don't know. I will thouroughly read the link. I only knew about the breakpoint. Thanks!

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 11월 13일
You do not initialize the profile variable. You assign to it inside of for loops that are potentially not being run, such as if periodsnumber < 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by