Function fails when called from another function

2 visualizaciones (últimos 30 días)
Miquel
Miquel el 13 de Nov. de 2019
Comentada: Miquel el 14 de Nov. de 2019
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 comentarios
the cyclist
the cyclist el 13 de Nov. de 2019
Editada: the cyclist el 13 de Nov. de 2019
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 el 14 de Nov. de 2019
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!

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Nov. de 2019
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

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by