フィルターのクリア

Save Cell data to file

4 ビュー (過去 30 日間)
monkey_matlab
monkey_matlab 2015 年 11 月 20 日
コメント済み: monkey_matlab 2015 年 11 月 20 日
Hello,
In the code below, how do I go about saving the cell data to a csv file with the specified headers?
Here is my code:
clc;clear;
A = 1:15;
B = (0.5)*rand(15,1);
C = 1:20;;
D = (0.5)*rand(20,1);
E = (0.5)*rand(20,1);
CELL{1} = A';
CELL{2} = B;
CELL{3} = C';
CELL{4} = D;
CELL{5} = E;
% fileID = fopen('check.dat','w');
% fprintf(fileID,'%6s %6s %6s %6s %6s\r\n','Iter1','b', 'Iter2', 'd', 'e');
% fprintf(fileID,'%6.5f %6.5f %6.5f %6.5f %6.5f\r\n',CELL);
% fclose(fileID);
  3 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 20 日
monkey_matlab
monkey_matlab 2015 年 11 月 20 日
編集済み: monkey_matlab 2015 年 11 月 20 日
I wanted to have the last entries empty in the first two columns. Thanks.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 20 日
Create a cell array large enough to hold everything if everything was the same length -- so in this case, 20 by 5. Set all the entries to '' the empty string. Now in the entries that should have a defined value, write in the string corresponding to the number that goes in that position. For example,
Astr = cellstr(num2str(A.', '%6.5f'));
Output(1:length(Astr), 1) = Astr;
Once the entire Output cell array has been populated,
fmt = '%s,%s,%s,%s,%s\n';
OutTranspose = Output .'; %need to transpose it
fprintf(fileID, fmt, OutTranspose{:});
The transpose has to do with row versus column concerns.
  1 件のコメント
monkey_matlab
monkey_matlab 2015 年 11 月 20 日
THANK YOU very much!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 11 月 20 日
編集済み: Image Analyst 2015 年 11 月 20 日
Try this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
A = 1:15;
B = (0.5)*rand(15,1);
C = 1:20;;
D = (0.5)*rand(20,1);
E = (0.5)*rand(20,1);
CELL{1} = A';
CELL{2} = B;
CELL{3} = C';
CELL{4} = D;
CELL{5} = E;
fileID = fopen('check.dat','wt');
fprintf(fileID,'%6s, %6s, %6s, %6s, %6s\n','Iter1','b', 'Iter2', 'd', 'e');
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{1});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{2});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{3});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{4});
fprintf(fileID,'%6.5f, %6.5f, %6.5f, %6.5f, %6.5f\n',CELL{5});
fclose(fileID);
  4 件のコメント
monkey_matlab
monkey_matlab 2015 年 11 月 20 日
Hello, this is the output I would like to get...
Iter1 b Iter2 d e f
1, .05120, 1, .8512, .5656, 5632
2, .56355, 2, .5635, .1259, .6321
...
15, .1256, 15, .8632, .4425, .0856
16, .2265, .4863, .9652
...
20, .1125, .9965, .1256
Thank you for your time.
Walter Roberson
Walter Roberson 2015 年 11 月 20 日
Is your goal to have column-oriented output, or is your goal to have a csv file? Because a csv file would look like
"Iter1","b","Iter2","d","e","f"
1,.05120,1,.8512,.5656,5632
2,.56355,2,.5635,.1259,.6321
...
15,.1256,15,.8632,.4425,.0856
,,16,.2265,.4863,.9652
...
,,20,.1125,.9965,.1256

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by