Fastest Way to write data to a text file - fprintf
Ältere Kommentare anzeigen
I am writing a lot of date to a text file one line at a time (1.7 million rows, 4 columns) that is comprised of different data types. I'm wondering if there is a better way to do this than 1 line at a time that might yield much faster results.
Here is what I'm doing now.
ExpSymbols = Char Array
ExpDates = Numeric Array
MyFactor = Numeric Array
FctrName = Char Array
ftemp = fopen('FileName','w' );
for i = 1:length(MyFactor)
fprintf(ftemp, '%s,%i,%f,%s\r\n',ExpSymbols(i,:), ExpDates(i,1), MyFactor(i,1),[FctrName '_ML']);
end
fclose(ftemp);
Thanks in advance,
Brian
Akzeptierte Antwort
Weitere Antworten (1)
It's a pita for mixed fields--I don't know of any clean way to mix them in fprintf c
I generally build the string array internally then write the whole thing...
cma=repmat(',',length(dates),1); % the delimiter column
out=[symb cma num2str(dates) cma factor cma names];
fprintf(fid, '%s\n', out);
fid=fclose(fid);
names is a placeholder for the FactorName that I guess may be a constant? If so, it can be inserted into the format string as Jan assumed; if not needs to be built as the column of commas to concatenate however it should be.
6 Kommentare
Brian
am 3 Aug. 2013
I haven't done a test but I'd be real surprised if conversion in memory of full arrays isn't quite a lot faster than a loop record-at-a-time. But, it could happen I suppose.
Certainly the faster way would be to not write such large files as formatted but as stream -- who's going to be looking at such a large dataset, anyway?--and if they do need to, then use a viewing helper app.
Since I'm here now, I'll comment on your ? on Jan's comment... :)
The 'W' will leave the buffering/flushing of output buffer up to the OS rather than forcing it after each call -- it probably will help a little for a very large file.
I'm not sure about the JIT compiler; but I'd expect it to have parsed the constant format string and so I'd not expect any difference between those two constructions--again the proof is always in the timing, of course. I suspect Jan just did it for readability as much as anything.
Brian
am 4 Aug. 2013
Yeah, but why do they have to be formatted instead of stream?
You can't change the other input form for the other routine or just do that in Matlab, too, w/o having to write the files in between?
I did a little test but my machine is very old and memory limited -- before I ran out of memory it appeared to me that the in memory process helped but you can't use num2str except w/ a fixed format because it'll have different numbers of significant digits otherwise.
ADDENDUM--I reverted back to R12 to get a little more memory available for data w/o thrashing disk.
Turns out that at least there num2str() lives up to it's reputation as a performance dog--the loop beat the internal conversion hands down for larger sizes. OTOMH I can't think of another builtin way to generate the columns w/o looping constructs--sprintf() embeds a \n if use it which is ok for display purposes but not for output to file. I guess I don't have any other answer than to see if can use stream i/o instead, sorry. OBTW, the other thing that will help if you still must write it as formatted -- once you do the conversion in memory, then use fwrite to output the data.
Brian
am 5 Aug. 2013
dpb
am 5 Aug. 2013
Also called "binary". It's unformatted i/o which has the benefits for speed of
a) full precision for float values at minimum number of bytes/entry, b) eliminates the format conversion overhead on both input and output
doc fwrite % and friends
or if could stay in Matlab then
doc save % and load is only slightly higher-level
The possible disadvantage is, of course, you can't just look at a file and read it; but who's going to manually be looking at such large files, anyway?
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!