MATLAB Answers

Exporting mixed cell array as textfile

2 views (last 30 days)
012786534
012786534 on 14 Jun 2017
Answered: Stephen Cobeldick on 14 Jun 2017
Hello all,
Let's say I had a mixed cell array containing numbers, characters and empty cells. Like so:
A 9 [empty] [empty]
Z 8 [empty] [empty]
C D E F
1 2 3 4
5 6 7 8
How could I export such a cell array as a textfile ?
Thank you

  1 Comment

Sign in to comment.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 14 Jun 2017
Perhaps something like this:
C = {'A',9,[],[];'Z',8,[],[];'C','D','E','F';1,2,3,4;5,6,7,8};
D = cellfun(@num2str,C','uni',0);
fmt = repmat(',%s',1,size(C,2));
fmt = sprintf('%s\n',fmt(2:end));
[fid,msg] = fopen('temp1.csv','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,D{:});
fclose(fid);
Which generates this file:
A,9,,
Z,8,,
C,D,E,F
1,2,3,4
5,6,7,8

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!