Delay in while loop
Afficher commentaires plus anciens
Hi,
I have a script:
While flag
If exist(file)
Read text
...
End
End
I have a folder that create a new file every 2.5/3 seconds , and the idea is that the loop will check if the new file was created and if so continue. Now when running in RT it looks like the while loop is being called only every 5 sec, meaning instead of reading a new file every 3 sec, I get 2 files read every 5 sec.
Do you any idea how to resolve this? Or is it possible to have another matlab function run simultaneously to read the folder for new file and send it via udp and than using fscan in the while?
Many thanks, Yael
7 commentaires
Sindar
le 12 Jan 2020
First a question: (why) do you need each file read in as soon as it's created, vs reading multiple in at a time (either 2 / 5sec or several later)?
Also, have you checked to ensure nothing else in the loop is taking longer than you'd expect?
Yael Hamrani
le 13 Jan 2020
Modifié(e) : Yael Hamrani
le 13 Jan 2020
Rik
le 13 Jan 2020
Is your code fast enough to keep up with the creation rate?
Sindar
le 13 Jan 2020
I'm still not quite sure I understand why the read time needs to be exact.
If you simply want to make sure that your data is stamped with the correct time, recording when it is read in is not the best way to do this. Instead, I'd look at the file data:
replace
t = fix(clock);
sprintf('%d:%d:%d ',t(4), t(5), t(6))
with
tmp = dir(sprintf('%s%d%s','File',TR_id,'.rtp'));
t = datetime(tmp.datenum,'ConvertFrom','datenum','Format','HH:mm:ss');
sprintf('%s',t)
Yael Hamrani
le 14 Jan 2020
Sindar
le 16 Jan 2020
First question: if you don't have data coming in, how often does the while loop cycle? If it's slower than you'd expect, check the run-and-time and see where the delay is.
Does whatever program is generating the files close out quickly? Maybe there's a delay before Matlab can access the data.
Is it possible the delay is related to your filesystem? Maybe it's taking longer to copy than you think. Maybe there's a delay before Matlab sees the files (and using an in-Matlab copy circumvents this). I don't know much that you could do about this, but it's an issue I've heard about before. My only suggestion would be to change out the if exist(~) check. Maybe just try-catch instead. It's possible fopen sees files sooner than exist().
Yael Hamrani
le 16 Jan 2020
Réponses (1)
Yael Hamrani
le 13 Jan 2020
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!