Call of textscan or strfind function takes long time for first time while running in loop [matlab 2020b on linux centos]
3 次查看(过去 30 天)
显示 更早的评论
We are trying to read a file which has repeatedly 9 rows of text followed by 200000 particle trajectories. A piece of code, enough to produce the problem is:
fname = abc.txt;
fid = fopen(fname,'r');
for ii = 1:20
InputText=textscan(fid,'%s',9,'delimiter','\n');
ii
pp = InputText{1};
Nrows = pp{4}
aa =textscan(fid,'%d %d %f %f %f %f', ... % Read data block
'delimiter',',');
end
apparently, to print ii = 1, it takes 10 seconds while rest all calls in loop finish in 1-2 seconds. It clearly indicates that trouble is on first call of textscan and not with subsequent calls.
The same issue is with strfind function if i put in loop.
I checked on matlab and the answers suggest either license issue or graphics issue. I ran matlab with no-desktop and still got the same issue. I checked it with Nvidia and opengl software choice and issue persists.
Kindly help resolving.
1 个评论
dpb
2021-7-14
InputText=textscan(fid,'%s',9,'delimiter','\n');
ii
pp = InputText{1};
Nrows = pp{4}
looks peculiar -- you read as char() data but then use the content as numeric without a conversion operation.
采纳的回答
Walter Roberson
2021-7-14
Specify the file encoding when you do the fopen(), or else it will scan the entire file checking to see if any utf8 is present
2 个评论
dpb
2021-7-14
I'm always forgetting that change in behavior, too...glad your rememberer is better, Walter! :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!