how to make a response for more than one period?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ani Asoyan
el 9 de Mzo. de 2020
Comentada: Guillaume
el 10 de Mzo. de 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 comentarios
Ameer Hamza
el 9 de Mzo. de 2020
Where are the iterations? There is no for or while loop in this code.
Respuesta aceptada
Guillaume
el 9 de Mzo. de 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 comentarios
Guillaume
el 10 de Mzo. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!