Function fails when called from another function
2 ビュー (過去 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
2019 年 11 月 13 日
編集済み: the cyclist
2019 年 11 月 13 日
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.
採用された回答
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 Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!