How to limit number of repetition
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
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.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Desktop dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!