I have some code that takes measurements from an external device and I want to take measurements for a set amount of time. I need to record the measurements along with timestamps, so I put together a while loop to run during execution of the timer, with the condition to exit the loop when the timer reaches zero.
The "meter" variable is the handle to the external device, and talking to it is not a problem.
The issue is that the while loop never exits when the code is executed within a function, even though the "Finished!" method successfully displays. The code works fine if I run it alone in the command window, and there are no other instances of timers running in the code, so I have no idea why this different behavior occurs. I would use a pause to keep track of the time, but I want to take as many readings as possible during the 10 seconds, so that just won't fly.
fprintf('\nCollecting 10 s data from detector...');
i = 1;
t1 = timer('TimerFcn','stat=false; fprintf(''\nFinished!'')','StartDelay',10);
start(t1);
stat = true;
while(stat==true)
Timestamps{i} = datetime('now');
fprintf(meter,':READ?');
Current(i) = str2num(fscanf(meter));
i = i + 1;
end
Any thoughts on why this might behave differently when within a function?