MATLAB Answers

Quick way to write cell or cell array

33 views (last 30 days)
Pearl
Pearl on 24 Aug 2012
Hi -
I'm trying to write a cell array containing something like (to a file):
'A1' 'A2' 'A3'
[2291x1 double] [1424x1 double] [1545x1 double]
Is there any quick and easy way to do so?
I appreciate any suggestion.
Thank you
Pearl

  0 Comments

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 24 Aug 2012
T={'A1' ,'A2' , 'A3';rand(2291,1) rand(1424,1) rand(1545,1)}
%i m not sur if that what you want

  4 Comments

Show 1 older comment
Azzi Abdelmalek
Azzi Abdelmalek on 25 Aug 2012
% you did'nt specify what kind of file. if excel file you can use
T={'A1' ,'A2' , 'A3';rand(2291,1) rand(1424,1) rand(1545,1)}
c='ABC'
for k=1:3
n=length(T{2,k})+1
xlswrite('ans_xls126.xlsx',T(1,k),sprintf([ c(k) '%d:' c(k) '%d'],1,1))
xlswrite('ans_xls126.xlsx',T{2,k},sprintf([ c(k) '%d:' c(k) '%d'],2,n))
end
Pearl
Pearl on 27 Aug 2012
Azzi -
Thank you! I thought I could get a way without looping through all the data I have since I either have 96 or 384 elements instead of just 'A1' to 'A3'. I guess it's just faster to loop through instead of trying to find a faster way.
Thanks again!
Azzi Abdelmalek
Azzi Abdelmalek on 28 Aug 2012
%for mor columns (676) you can use this:
T={'A1' ,'A2' , 'A3';rand(2291,1) rand(1424,1) rand(1545,1)}
s='ABCDEFGHIJKLMNOPQRSTUVWXYZ';c=s';
for k=1:length(s)
for l=1:length(s)
c=char(c,[s(k) s(l)])
end
end
for k=1:size(T,2)
n=length(T{2,k})+1;ch1=strtrim(c(k,:))
xlswrite('ans_xls126.xlsx',T(1,k),sprintf([ ch1 '%d:' ch1 '%d'],1,1))
xlswrite('ans_xls126.xlsx',T{2,k},sprintf([ ch1 '%d:' ch1 '%d'],2,n))
end

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 2012
MATLAB is a bit weak on routines to write cell arrays all at one time. But you can use something like this:
headers = YourCell(1,:);
header_format = '%.15s %.15s %.15s\n';
values = cell2mat(YourCell(2,:));
numcols = size(values,2);
value_format = [ repmat('%.15g ', 1, numcols-1), '%.15g\n');
fout = fopen('YourFile.txt', 'wt');
fprintf(fout, header_format, headers{:});
fprintf(fout, value_format, values.'); %transpose is important!
fclose(fout);

  2 Comments

Pearl
Pearl on 25 Aug 2012
@Walter
Thank you for your suggestion. The problem I have is that I can't do cell2mat(YourCell(2,:)) since each element of mine has different dimension. (Please see an example I posted). How can I write column by column? Or else I should create a matrix with the size of maximum numel of all the cell element I have, and then fill with NaN. Will NaN be written to file?
Walter Roberson
Walter Roberson on 25 Aug 2012
NaN will be written to the file if you use the structure such as I showed.
In order to write in "columns", you need to define how you can tell columns apart, such as how you can tell whether there are one blank columns or two blank columns instead. When that is defined we can help write the code.

Sign in to comment.

Sign in to answer this question.