How to limit number of repetition

2 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2022-2-22
编辑:Jan 2022-2-22
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 个)

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by