How to limit number of repetition

2 views (last 30 days)
Davide Frattini
Davide Frattini on 22 Feb 2022
Edited: Jan on 22 Feb 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 Comment
Jan
Jan on 22 Feb 2022
This is not twitter: no # before the tags. Thanks.

Sign in to comment.

Accepted Answer

Jan
Jan on 22 Feb 2022
Edited: Jan on 22 Feb 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.

More Answers (0)

Categories

Find more on Downloads in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!