Saving a cell with multiple vectors of different length to csv or xlsx

20 views (last 30 days)
Hello everyone,
as the title says, I'm trying to save a cell structured like this (1x1x27):
val(:,:,1) =
[1323×1 double]
val(:,:,2) =
[1117×1 double]
val(:,:,3) =
[1162×1 double]
% There are n=27+ columns of x length, so for simplicity i just wrote n
% and x in order not to have to paste too much text
val(:,:,n) =
[x×1 double]
in a csv file, because then i need to manually check the graphs obtained by each columns.
The cells only contains numbers.
I managed to create such a file already, but i used the xlswrite function so the code was too slow, and i want to make it faster.
Do you have any ideas on how i can proceed?
To summarise: I have multiple vectors of different length, and i want to save them in a single matrix/csv file.
Thanks in advance!

Accepted Answer

dpb
dpb on 10 Feb 2019
The easier way but does open/close the file every time...
N=size(val,3);
rOff=0;
for i=1:N
v=val{:,:,i}; % dereference the cell array
csvwrite('youroutputfile,csv',v,rOff,0); % write with offset
rOff=rOff+numel(v):
end
The more efficient way as far as file access but requires you to do more of the work/formatting...
fid=fopen('youroutputfile,csv','w');
N=size(val,3);
for i=1:N
v=val{:,:,i}; % dereference the cell array
fmt=[repmat('%g,',1,numel(v)-1),'%g\n']; % build the appropriate format string
fwrite(fid,fmr,v); % write with offset
end
fid=fclose(fid);
Salt to suit the desired format string, etc., ...

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!