Hi all,
I wish to write a some information in form of a csv file from a matlab code.
In the MATLAB code I have stored the header as a cell array:
ToCSV={'Location' 'Weight_factor' 'Average' 'Maximum' 'Minimum'};
I append rows to this cell array by looping. A sample statement is:
ToCSV={ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} };
I wish to print this as a csv file. I used following routine which sort of giving errors:
fid = fopen('outfile.csv','w');
fprintf(fid,'%s, %s, %s, %s, %s\n',ToCSV{1,:});
fprintf(fid,'%s, %f, %10.2e, %10.2e, %f\n',ToCSV{2:end,:});
fclose(fid);
>>Error using fprintf
>>Function is not defined for 'cell' inputs.
Can some of you please provide pointers to achieve this ? I also tried csvwrite but apparently it doesn't go well with cell arrays.
Thanks a lot !!!

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 5일

0 개 추천

you should concatenate like this
ToCSV=[ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} ];

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 12월 5일

0 개 추천

Change
ToCSV={ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} };
to
ToCSV=[ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} ];

댓글 수: 3

Yeah both you folks are right and thanks for help. Yet I am facing some problem as I am populating my location entry through a regexp search:
Location=regexp(all_files(i).name,expression,'match'); This entry is filled into 1st column of ToCSV cell array.
My final ToCSV is like:
>> ToCSV
ToCSV =
'Location' 'Weight_factor' 'Average' 'Maximum' 'Minimum'
{1x1 cell} [ 0.0500] [9.7449e-07] [986.7844] [ 87.6252]
{1x1 cell} [ 0.5000] [8.6915e-06] [507.5409] [107.7545]
....
So when I run the same code I get:
>>Error using fprintf
>>Function is not defined for 'cell' inputs.
I tried to use cell2mat during concatenation:
ToCSV=[ToCSV; {cell2mat(Location) 0.5 1e+3 1e+4 1e+2} ];
The code then went fine as I got following content of ToCSV:
ToCSV =
'Location' 'Weight_factor' 'Average' 'Maximum' 'Minimum'
'A1' [ 0.0500] [3.0933e-06] [ 847.4803] [ 94.7178]
'A1' [ 0.5000] [2.8002e-05] [ 740.9295] [109.7109]
'A2' [ 0.5000] [2.7834e-05] [1.5684e+03] [ 89.2992]
'A2' [ 0.0500] [3.0045e-06] [2.9597e+03] [ 77.9242]
'A3' [ 0.0500] [9.7449e-07] [ 986.7844] [ 87.6252]
'A3' [ 0.5000] [8.6915e-06] [ 507.5409] [107.7545]
But the fprint command kind of messed the order of elements in csv file and I didn't get the same tabular format of ToCSV (shown above)
Contents of csv file:
Location Weight_factor Average Maximum Minimum
A1 65 4.90E+01 6.50E+01 50
A2 65 5.10E+01 6.50E+01 51
5.00E-02 0.5 5.00E-01 5.00E-02 0.05
5.00E-01 0.000003 2.80E-05 2.78E-05 0.000003
9.74E-07 0.000009 8.47E+02 7.41E+02 1568.418126
2.96E+03 986.784402 5.08E+02 9.47E+01 109.710851
8.93E+01 77.924157 8.76E+01 1.08E+02
Need some more help from you folks to identify the bug here.
Thanks a lot !!!
What do you want to appear in the first column in the csv file?
If you want the cell array of strings to appear, separated by spaces, then
str_to_store = sprintf('%s ', ToCSV{row,1});
You can automate this with something like
FirstCol = cellfun( @(C) sprintf('%s ', C{:}), ToCSV(:,1), 'Uniform', 0);
Rahul  Pandey
Rahul Pandey 2013년 12월 5일
Got it !! Thanks !

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

질문:

2013년 12월 5일

댓글:

2013년 12월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by