2010-12-30 12 views
2

私は以前に約including matrices and strings in a .txt fileを照会しました。私は今それに細胞を追加する必要があります。私の以前の質問から:セル配列を.txtファイルに追加するにはどうすればよいですか?

str = 'This is the matrix: '; 
mat1 = [23 46; 56 67]; 
fName = 'output.txt'; 
fid = fopen(fName, 'w'); 
if fid >= 0 
    fprintf(fid, '%s\n', str); 
    fclose(fid); 
end 
dlmwrite(fName, mat1, '-append', 'newline', 'pc', 'delimiter', '\t'); 

は今、私は、文字列を追加したい:「削除識別子はある」とそれ以下、このセル・アレイ:

'ABC' [10011] [2] 
'DEF' [10023] [1] 

いくつかの関連リンク:

http://www.mathworks.com/help/techdoc/ref/fileformats.html,http://www.mathworks.com/support/solutions/en/data/1-1CCMDO/index.html?solution=1-1CCMDO

答えて

3

残念ながら、データのセル配列の書き込みにDLMWRITEまたはCSVWRITEのような関数を使用することはできません。しかし、出力を得るためには、FPRINTFへの1回の呼び出しを使用できますが、セル配列の行のすべてのエントリの形式を指定する必要があります。このようになります。

str = 'The removed identifiers are: '; %# Your new string 
cMat = {'ABC' 10011 2; 'DEF' 10023 1}; %# Your cell array 
fid = fopen(fName,'a');     %# Open the file for appending 
fprintf(fid,'%s\r\n',str);    %# Print the string 
cMat = cMat.';       %'# Transpose cMat 
fprintf(fid,'%s\t%d\t%d\r\n',cMat{:}); %# Print the cell data 
fclose(fid);        %# Close the file 

そして(旧例を含む)は、新しいファイルの内容::my answer to your previous questionを踏まえ、あなたはこれらの追加の行を追加します

This is the matrix: 
23 46 
56 67 
The removed identifiers are: 
ABC 10011 2 
DEF 10023 1 
0

cellwriteからFile Exchange。 Reading Writing Mixed Data With MATLABからFrancis Barnhart、cellwriteの作成者は一見価値があるかもしれません。

ファイルハンドルを受け入れるようにcellwriteの署名を変更することは、実行可能な作業である必要があります。既存のファイルにデータを追加できるようにする。