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

조회 수: 20 (최근 30일)
Lorenzo De Santis
Lorenzo De Santis 2019년 2월 10일
답변: dpb 2019년 2월 10일
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!

채택된 답변

dpb
dpb 2019년 2월 10일
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., ...

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by