how to make fscanf faster
Ältere Kommentare anzeigen
Hi, all
I have problem with getting data from a sensor using serial communication rs-232. The problem is fscanf takes much time. There is profiler result below.
time calls for i=1:1:1501
2.61 1501 Out(i,1) = eval(fscanf(s));
0.01 1501 end
The sensor provides data(13X1 char) when it is called. I used a for-loop to acquire whole data (1501X13)
1501 calls increase its time. If I reduce the number of calls, the accuracy will be reduced. I want to have high accurate data that means I need to call 1501 times. Is there any way to make fscanf faster? OR any better code to get data faster in serial communication?
Thanks in advance.
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 29 Jan. 2013
2 Stimmen
You do not know you are testing the speed of fscanf(): you might be testing the speed of eval(). Are you certain you need eval?
9 Kommentare
Jong-Hwan Kim
am 30 Jan. 2013
Walter Roberson
am 30 Jan. 2013
Do you mean characters such as 'Pi/7' ? Or do you mean characters such as '0.9258e+04' ? If you mean the later, then use fscanf(s, '%f'). If the number of numbers per line is consistent then it is more efficient to repeat the '%f', as in fscanf(s, '%f%f%f%f%f') for 5 space-separated numbers.
EDIT: please, discard this comment and see Jan's comment below!
Following up on Walter comment, you could generate a format string for e.g. 1501 floats with:
fmt = char(repmat('%f', 1, 1501)) ;
values = fscanf(fid, fmt) ;
Jan
am 30 Jan. 2013
@Cedric: You can omit the "char()", because repmat('%f', 1, 1501) replies a char already. Is this faster than fscanf(fid, '%f', [1, 1501])?
Cedric
am 30 Jan. 2013
Jan, to be honest, I had forgotten that fscanf can take a size arg!
Jan
am 30 Jan. 2013
I do not have access to a Matlab computer currently, so I'm not sure which method is faster. A naive C-mex approach to interpret ASCII strings from a file as a decimal number can be much faster than Matlab's FSCANF, see e.g. FEX: str2doubleq. But unfortunately the results are not reliably, e.g. for exceptions like NaN, Inf, malformed numbers, and for numbers which cannot be represented as binary values exactly. Then e.g. the parser of Googles V8 engine is much smarter: http://code.google.com/p/double-conversion/ . But another problem remains, that I do not know an efficient method to access a file from inside a Mex-function, which has been open from Matlab. Therefore I cannot simply plug in the V8 code to create a faster FREAD.
Jong-Hwan Kim
am 30 Jan. 2013
Bearbeitet: Jong-Hwan Kim
am 30 Jan. 2013
Jong-Hwan Kim
am 30 Jan. 2013
Bearbeitet: Jong-Hwan Kim
am 30 Jan. 2013
Mo
am 1 Mai 2014
0 Stimmen
Has anybody a new answer to the latest question here? Why this error might be produces "Unsuccessful read: OBJ must be connected to the hardware with FOPEN." while the port is already open and ready to use?
1 Kommentar
Jan
am 3 Mai 2014
Please open a new thread for a new question.
Kategorien
Mehr zu Whos finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!