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
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
Yael Hamrani 2020년 1월 13일
편집: Yael Hamrani 2020년 1월 13일
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 2020년 1월 13일
Is your code fast enough to keep up with the creation rate?
Sindar
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
Yael Hamrani 2020년 1월 14일
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 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
Yael Hamrani 2020년 1월 16일
Thank you Sindar! I will try that :) fingures croosed.

댓글을 달려면 로그인하십시오.

답변 (1개)

Yael Hamrani
Yael Hamrani 2020년 1월 13일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2020년 1월 12일

댓글:

2020년 1월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by