Function fails when called from another function

1 次查看(过去 30 天)
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