フィルターのクリア

writing a matrix into a .txt file

2 ビュー (過去 30 日間)
Oded Scharf
Oded Scharf 2018 年 2 月 13 日
編集済み: Stephen23 2018 年 2 月 13 日
I have a matrix of 65x10 and i want to open and write 9 txt files (from 2:10) each named by the first row at its column and contains two columns: the first column is the first column of the matrix (contains str) and the second one is the i'th column of the matrix (contains int) (starting from the second row). Also I need a title for both columns in each file.
Thanks, Oded

採用された回答

Stephen23
Stephen23 2018 年 2 月 13 日
編集済み: Stephen23 2018 年 2 月 13 日
You do not specify what kind of matrix you have, so I will assume that it is a cell array (as a numeric array would not hold string/char data). This code should get you started, please experiment and adapt as required:
P = '.'; % directory path
C = 65x10 cell array
for k = 2:size(C,2)
N = sprintf('column_%d.csv',k);
[fid,msg] = fopen(fullfile(P,N),'wt');
assert(fid>=3,msg)
fprintf(fid,'%s,%s\n',C{1,[1,k]});
tmp = C(2:end,[1,k]).';
fprintf(fid,'%s,%d\n',tmp{:});
fclose(fid);
end
  2 件のコメント
Oded Scharf
Oded Scharf 2018 年 2 月 13 日
thank you! it really helped
Stephen23
Stephen23 2018 年 2 月 13 日
編集済み: Stephen23 2018 年 2 月 13 日
@Oded Scharf: I hope that it helps. Please ask if you have more questions. You should also accept the answer that helps to resolve your original question, as this shows to others that your question has been resolved.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by