Delay in while loop
古いコメントを表示
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 件のコメント
Sindar
2020 年 1 月 12 日
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
2020 年 1 月 13 日
編集済み: Yael Hamrani
2020 年 1 月 13 日
Rik
2020 年 1 月 13 日
Is your code fast enough to keep up with the creation rate?
Sindar
2020 年 1 月 13 日
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
2020 年 1 月 14 日
Sindar
2020 年 1 月 16 日
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
2020 年 1 月 16 日
回答 (1 件)
Yael Hamrani
2020 年 1 月 13 日
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!