Call of textscan or strfind function takes long time for first time while running in loop [matlab 2020b on linux centos]
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sanat Kumar Tiwari
il 14 Lug 2021
Commentato: dpb
il 14 Lug 2021
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 Commento
dpb
il 14 Lug 2021
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.
Risposta accettata
Walter Roberson
il 14 Lug 2021
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 Commenti
dpb
il 14 Lug 2021
I'm always forgetting that change in behavior, too...glad your rememberer is better, Walter! :)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Import and Export in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!