Delay in while loop

1 view (last 30 days)
Yael Hamrani
Yael Hamrani on 12 Jan 2020
Commented: Yael Hamrani on 16 Jan 2020
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 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
Yael Hamrani on 16 Jan 2020
Thank you Sindar! I will try that :) fingures croosed.

Sign in to comment.

Answers (1)

Yael Hamrani
Yael Hamrani on 13 Jan 2020
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

Community Treasure Hunt

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

Start Hunting!