MATLAB Answers

how to make a response for more than one period?

1 view (last 30 days)
Ani Asoyan
Ani Asoyan on 9 Mar 2020
Commented: Guillaume on 10 Mar 2020
function response = f(n,p)
if (p == 1)
response = 'yes';
elseif (p == 2)
response = 'no';
end
This is my function.
n is the number of iterations.
I want to make a response with memory. For example
if (p==1), response will be 'yes' for 2 iterations, then it will be 'no' regardless what p would be

  2 Comments

Ameer Hamza
Ameer Hamza on 9 Mar 2020
Where are the iterations? There is no for or while loop in this code.
Ani Asoyan
Ani Asoyan on 9 Mar 2020
This code is linked to another code in which n is the number of iterations... but I want to write a loop in this one with n so that I can make my code as I described above

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 9 Mar 2020
Your question is still very unclear. I think you're saying that in another function you've got a loop that calls your function passing p and n, the latter being the loop index. In which case, it's as simple as:
function response = f(p, n) %much better function name required! Use full words that describe what the function does
if p == 1 & n < 3
response = 'yes';
else %happens when p is not 1 or from the 3rd iteration
response = 'no';
end
end
A few notes about the code you've written:
  • in matlab you don't need to enclose an if condition in (). Personally, I find that wrapping if condition in () make the code harder to read.
  • Never end an if on an elseif. The code you wrote would error (with "output response not assigned") if p is anything other than the scalar 1 or 2. At least my code (which ends with an else not an elseif) will return 'no'.
  • Use a better name for your function and write comment. Writing code that can be understood by others is just as important as writing code that works.

  6 Comments

Show 3 older comments
Guillaume
Guillaume on 9 Mar 2020
So, do I get this right: At the start of the loop (n = 0), p is 0. At some point (let's call it n = ), p will go to 1 and you want the function to return 'yes' for two iterations ( and ) . After that, the output is constantly 'no' regardless of the value of n and p.
In effect you want to implement a one-shot latch that stays on for two cycles.
Ani Asoyan
Ani Asoyan on 10 Mar 2020
Yes, a little correction. at n=0 p is not zero, it is -1 ( in fact I can give it whatever I want)
when p goes to 1 at t-th iteration, function will return 'Yes' in the i-th and (i+1)-th iteration. Then the output will be 'No' as long as p has other value than 1. when p will be equal to 1 again, then again in that two iterations function will return 'Yes'
Guillaume
Guillaume on 10 Mar 2020
Ok, you just want to implement a latch for two cycles. This requires the function to have memory of a state between calls. That's not typical but can be implemented with persistent variables. However, if you want to keep things simple, particularly as the function does so little, you may be better off implementing the latching directly in your loop:
%... main code that does the looping
latch_countdown = 0; %default state when not latched
latch_duration = 2; %duration of latch in loop iterations. Includes the iteration when it is set. Hence 2 is n and n+1
for n = 0:whatever %could be a while loop as well. Doesn't matter
%... code that sets p
if p == 1
latch_countdown = latch_duration; %start latch
end
if latch_countdown > 0
latch_countdown = latch_countdown - 1;
response = 'yes';
else
response = 'no';
end
%...
end
Now, if you do want to do the same in a function, as said you have to use a persistent variable:
function response = latched_response(p, latch_duration)
%latch for latch_duration iterations (n), when p is 1.
%latch_duration is optional, default value is 2.
persistent latch_countdown;
if nargin < 3
latch_duration = 2; %default value if not specified
end
if isempty(latch_countdown) %first call to the function
latch_countdown = 0;
end
if p == 1
latch_countdown = latch_duration; %start latch
end
if latch_countdown > 0
latch_countdown = latch_countdown - 1;
response = 'yes';
else
response = 'no';
end
end
with your main code being more or less:
latch_duration = 2; %duration of latch in loop iterations. Includes the iteration when it is set. Hence 2 is n and n+1
for n = 0:whatever %could be a while loop as well. Doesn't matter
%... code that sets p
response = latched_response(p, latch_duration);
%...
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!