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 Comments

Sindar
Sindar on 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
Yael Hamrani on 13 Jan 2020
Edited: Yael Hamrani on 13 Jan 2020
Hi,
I need the time rate to be the same as the file creation, I am working in real time, meaning the time the file is created must be exact, the only thing in the loop is :
While flag
If exist file
Str= read(file)
Else
Str=''
End
If ~strcmp(str,'')
...
End
End
So if there is no new file it is not suppose to go inside the second if.
Thanks, Yael
Rik
Rik on 13 Jan 2020
Is your code fast enough to keep up with the creation rate?
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)
Thank you for your replay the stamping is not important, however it is important to me to read the data in real time (as much us posibble delays of ms are OK). The stamping is a tool for me to know when was the reading was made and that is how I verifed that I have this delays.
Sindar
Sindar on 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().
Thank you Sindar! I will try that :) fingures croosed.

Sign in to comment.

Answers (1)

Hi,
So when I use timer function that transfers the files from one folder to the input folder every 3 sec
T=timer;
set(T,'Period',3,'executionMode','fixedRate','TimerFcn',@(~,~) movefile1 )
start(T)
and than run my function :
while (b_run==1 &&TR_id<=settings.total_TR)
if ~settings.flag
if exist(sprintf('%s%d%s','File',TR_id,'.rtp'))
[a, errmsg] = fopen(sprintf('%s%d%s','File',TR_id,'.rtp'), 'r+');
if ~isempty(errmsg)
warning('failed to open %s due to %s', sprintf('%s%d%s','File',TR_id,'.rtp'), errmsg); %or use error if you want the code to stop
else
a=fopen(sprintf('%s%d%s','File',TR_id,'.rtp'))
format long
aa=fscanf(a,'%f')
str_rcv=num2str(aa(2,1),'%3.6f');
fclose(a)
TR_id=TR_id+1;
end
elseif TR_id==0
str_rcv='NaN';
TR_id=1;
else
str_rcv='';
end
else
break
end
if (strcmp(str_rcv,'')~=1)
input_count= input_count+1
t = fix(clock);
sprintf('%d:%d:%d ',t(4), t(5), t(6))
val_rcv = str2double(str_rcv)
...
end
end
b_run=0;
I have val_rcv every 3 sec. when I don't use timer to move files and read files from origin folder where they are created every 3 sec I have a delays and files are being read every 5 sec.
So the problem is not in the function but in the way I read the files that are being originated. Do you have any suggestions for a solution like fscan to listen coniniously to the files that are being originated and reading their data real time.
for example :
file 1 originated in 12:44:01 >>str1
file 2 originated in 12:44:03>>str2
but In my registery:
12:44:03>>val_rcv=str1
12:44:03>>val_rcv=str2
instead of in real time.
Thanks,
Yael

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Jan 2020

Commented:

on 16 Jan 2020

Community Treasure Hunt

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

Start Hunting!