what is the fastest way to convert a cell array of delimited numbers into a matrix
4 views (last 30 days)
Show older comments
I have a cell array called 'out', which has 1 column and 500k rows and looks the following way:
1,5012,0,35,6
2,395,1,35,8
...
That is, each cell contains a string of numbers delimited by a comma. I'd like to obtain a matrix of numbers, D. I used the following script:
for i=1:size(out,1)
singline = (textscan(out{i},'%s','delimiter',','));
D(i,:) = str2double(singline{1});
end
It takes 206 seconds, which seems long, as this is just a small part of a big script to be repeated many times.
I also found out that I can use a function dlmcell taken from here: http://www.mathworks.com/matlabcentral/fileexchange/25387-write-cell-array-to-text-file/content/dlmcell.m
So if I use it in the script:
File = 'E:\result.csv';
dlmcell(File,out,',');
D = dlmread(File);
it takes only 31 seconds, and I get what I wanted, i.e. matrix D made of numbers
But it seems weird to me that a script with writing a file to a hard-drive and reading it back works faster than the one without that. So I'd like to ask what would be actually the fastest way to do this (using MATLAB 2010).
Thanks.
1 Comment
Answers (2)
Jos (10584)
on 22 Mar 2016
A = {'1,5012,0,35,6' ; '2,395,1,35,8'} ;
A = repmat(A,250000,1) ; % big array!
tic ;
A2 = strcat(A,',') ;
V = sscanf([A2{:}],'%f,') ;
V = reshape(V,5,[]).' ;
toc
% Elapsed time is 2.397291 seconds.
12 Comments
Sandeep Chaudhuri
on 4 Aug 2023
I am sorry, I forgot to attach the files. I can attach the file with 100 rows but the one with 10000 is like 144 MB and there is no way I can attach it..
Stephen23
on 4 Aug 2023
Edited: Stephen23
on 4 Aug 2023
" I can attach the file with 100 rows but the one with 10000 is like 144 MB and there is no way I can attach it.."
The file you attached has exactly one row, not 100 rows. It is very easy to import:
V = readmatrix('testpulses_100_08042023.txt')
Of course if the number of elements is suitable, there is nothing stopping you from reshaping it:
M = reshape(V,[],100).' % taking a guess about the order
If your imported data does not reshape, it is because the number of elements is not suitable.
Show us the outputs from these commands:
V = readmatrix('your huge data file.txt');
size(V)
and tell us the exact size you want to reshape it into.
Fangjun Jiang
on 22 Mar 2016
Edited: Fangjun Jiang
on 22 Mar 2016
a={'1,5012,0,35,6';'2,395,1,35,8'};
b=str2num(char(a))
b =
1 5012 0 35 6
2 395 1 35 8
a={'1,5012,0,35,6';'2,395,1,35,8'};
aa=repmat(a,250000,1);
tic;
b=str2num(char(aa));
toc
Elapsed time is 19.681015 seconds.
3 Comments
Fangjun Jiang
on 22 Mar 2016
You mean it is slower than the dlmcell() approach? My comparison shows it is faster than the dlmcell() approach.
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!