Write cell array defined by a variable to a excel file
3 views (last 30 days)
Show older comments
Hello,
I have a cell array that I get after processing data which needs to be written to a excel file. For some reason, fprintf is not working. Niether is writecell. Here is the code:
c = [D1 D2];
fileID = fopen(sprintf('%s_final.txt',fname), 'w');
% fprintf(c,fileID);
fclose(fileID);
Does anyone know what I am doing wrong?
The cell array output is something like:
'0.34' '0.4883' '0.000498246' '1.005859' '201780.00' '0.000004956' ' 36.0' 23
'0.51' '0.4883' '0.000498246' '1.000977' '200800.00' '0.000004980' ' 36.0' 23
'0.68' '0.5078' '0.000518176' '1.000977' '193073.07' '0.000005179' ' 35.0' 23
'0.85' '0.5273' '0.000538106' '1.000977' '185918.51' '0.000005379' ' 35.0' 23
'1.02' '0.4980' '0.000508211' '1.005859' '197821.57' '0.000005055' ' 35.0' 23
'1.19' '0.4932' '0.000503229' '1.000977' '198810.89' '0.000005030' ' 35.0' 23
'1.36' '0.5029' '0.000513194' '1.005859' '195900.01' '0.000005105' ' 35.0' 23
'1.53' '0.5078' '0.000518176' '1.005859' '194015.37' '0.000005154' ' 35.0' 23
'1.70' '0.5029' '0.000513194' '1.000977' '194948.56' '0.000005130' ' 35.0' 23
'1.87' '0.4785' '0.000488281' '1.005859' '205900.00' '0.000004857' ' 35.0' 23
'2.04' '0.4688' '0.000478316' '1.000977' '209170.84' '0.000004781' ' 35.0' 23
'2.21' '0.4932' '0.000503229' '1.000977' '198810.89' '0.000005030' ' 35.0' 23
'2.38' '0.4980' '0.000508211' '1.000977' '196860.79' '0.000005080' ' 35.0' 23
'2.55' '0.5029' '0.000513194' '1.005859' '195900.01' '0.000005105' ' 35.0' 23
'2.72' '0.4980' '0.000508211' '1.005859' '197821.57' '0.000005055' ' 35.0' 23
'2.89' '0.4932' '0.000503229' '1.000977' '198810.89' '0.000005030' ' 35.0' 23
'3.06' '0.5078' '0.000518176' '1.005859' '194015.37' '0.000005154' ' 35.0' 23
'3.23' '0.5225' '0.000533123' '1.005859' '188572.90' '0.000005303' ' 35.0' 23
'3.40' '0.4932' '0.000503229' '1.000977' '198810.89' '0.000005030' ' 35.0' 23
'3.57' '0.4980' '0.000508211' '1.000977' '196860.79' '0.000005080' ' 35.0' 23
'3.74' '0.5029' '0.000513194' '1.005859' '195900.01' '0.000005105' ' 35.0' 23
'3.91' '0.5127' '0.000523158' '1.010742' '193100.00' '0.000005179' ' 35.0' 23
'4.08' '0.5029' '0.000513194' '1.000977' '194948.56' '0.000005130' ' 35.0' 23
'4.25' '0.4834' '0.000493264' '1.005859' '203819.18' '0.000004906' ' 35.0' 23
'4.42' '0.4736' '0.000483299' '1.005859' '208023.71' '0.000004807' ' 35.0' 23
'4.59' '0.4980' '0.000508211' '1.005859' '197821.57' '0.000005055' ' 35.0' 23
'4.76' '0.4980' '0.000508211' '1.005859' '197821.57' '0.000005055' ' 35.0' 23
'4.93' '0.5078' '0.000518176' '1.005859' '194015.37' '0.000005154' ' 35.0' 23
'5.10' '0.4834' '0.000493264' '1.000977' '202829.28' '0.000004930' ' 35.0' 23
'5.27' '0.4932' '0.000503229' '1.000977' '198810.89' '0.000005030' ' 35.0' 23
'5.44' '0.5078' '0.000518176' '1.005859' '194015.37' '0.000005154' ' 35.0' 23
Thanks in advance
0 Comments
Answers (2)
dpb
on 1 Sep 2019
"a cell array that I get after processing data which needs to be written to a excel file. For some reason, fprintf is not working."
c = [D1 D2];
fileID = fopen(sprintf('%s_final.txt',fname), 'w');
% fprintf(c,fileID);
fclose(fileID);
fprintf is documented to not accept arrays other than numeric or character, not cell arrays. Further, it "knows nuthink!" about Excel altho you open the file with the .txt extension which is not a recognized Excel-formatted file altho Excel can import properly formatted text files.
Further, in
fprintf(c,fileID);
you don't use recognized syntax -- the actual argument order for fprintf is (fileID,formatSpec,A1,...An). You put a cell array (unallowable anyway) to be written first, the file handle second (instead of first) and left out a format specification entirely.
As Adam notes, for cell arrays, use either writecell or xlswrite depending on release you're running.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!