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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by