How to limit number of repetition

2 vues (au cours des 30 derniers jours)
Davide Frattini
Davide Frattini le 22 Fév 2022
Modifié(e) : Jan le 22 Fév 2022
I would need to run this code a specific number of time only (lets say n=30) and at the 30th times the user press on the keyboard, the program stops and creare a text file with the "fr" value reached at the 30th trial.
Here is my code so far:
global p
esc=0;
fr = 20;
while esc ~= 27
d.write(NISignal(fr))
esc = double(p);
if p == '1'
fr = fr+1;
else
fr = fr-2;
end
end
function s = NISignal(freq)
global nxsec dur p
s = 2.6 + 1/4.5*cos(linspace(pi,(2*pi*freq*dur)+pi,nxsec*dur)');
plot(s);
text(1000,6.7,string(freq))
if waitforbuttonpress
p = get(gcf, 'CurrentCharacter');
end
  1 commentaire
Jan
Jan le 22 Fév 2022
This is not twitter: no # before the tags. Thanks.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 22 Fév 2022
Modifié(e) : Jan le 22 Fév 2022
Add a counter:
esc = 0;
num = 0;
while esc ~= 27 && num < 30
...
num = num + 1;
end
if num == 30
...save your file
end
By the way, communicating through global variables is a really bad design. Prefer to provide the p value as another output argument.

Plus de réponses (0)

Catégories

En savoir plus sur Desktop dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by