Making a program recursive
1 view (last 30 days)
Show older comments
Andrew Wileman
on 27 May 2015
Commented: Andrew Wileman
on 27 May 2015
Hi, I wonder if anyone may be able to help. I have a program I've written that is a sliding window, it basically sends chunks of data from(sample3) within the window to a function (grey_model_sw), this function outputs the variables [x,xs,yw]. Within the function, I have a variable t_test which at present is set manually to say 5000. What I would like to do is set t_test with the length of the last value to yw instead of having to set it manually. I have tried but failed to get this to work, any idea?
Help would be gratefully be appreciated.
Andy
0 Comments
Accepted Answer
Salaheddin Hosseinzadeh
on 27 May 2015
Hi Andy!
I'm not sure if I understood your problem, so let me explain what I understood first! And if it is not what you mean please explain a little bit more.
My understanding: You have a function that you pass variables to and you get some outputs out of it. You have a parameter fixed inside your function to 5000 which should be length of the previous out put of the same function
For instance
[x,xs,yw] = myfunction(input1,in2,in3)
% here you have some codes
t_test = 5000;
% rest of yoru code
% assigning the output variables for example
x = sin(input1);
xs = cos(in2);
yw = tan(in2);
end % this is the end of the function
but you want your function to be as
[x,xs,yw] = myfunction(input1,in2,in3,t_test)
% there is no constant t_test in body of the code
end
where t_test is = length(yw) of the last iteration!
So you can either put myfunction inside a loop and put this all inside another function if you like.
Or
Use a persistent variable! this variable does not remove when function is reached the end, so in the next iteration it has a value. Look at MATLAB help for persistent keyword.
I think your code should look like this eventually
[x,xs,yw] = myfunction(input1,in2,in3,in4)
% here you have some codes
%
if ~exist(t_test)
persistent t_test; defining a persistent variable
t_test = in4; this only happens the first time
end
% rest of yoru code
% assigning the output variables for example
x = sin(input1);
xs = cos(in2);
yw = tan(in2);
%
t_test = length(yw); here is setting the t_test for new iteration
%
end % this is the end of the function
This is a clue on how to do it, I'm sure you can figure it out.
Good Luck!
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!