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
  1 件のコメント
Miquel
Miquel 2019 年 11 月 14 日
thanks :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by