how can I append this format using dlmwrite ?

조회 수: 14 (최근 30일)
Eliza
Eliza 2018년 2월 9일
댓글: Walter Roberson 2018년 2월 12일
I wanna the result in the text file to be
1 2 4 A
5 6 7 B
8 9 0 C
I used this formula but the result is not what I want
A=[1 2 4;5 6 7;8 9 0] B=[A;B;C]
dlmwrite('DECALLFEATURES.csv',A,'-append', 'newline', 'pc');
dlmwrite('DECALLFEATURES.csv',B,'delimiter','','-append','newline', 'pc');
  댓글 수: 1
Eliza
Eliza 2018년 2월 9일
could I make it in one line of code?

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

답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 9일
dlmwrite() cannot be used to output a mix of text and numeric on the same call.
dlmwrite() does not like to output text at all. You can trick it into handling text by setting the Delimiter property to empty, and set 'precision' to '%s', and pass in a character array with one row per line in which each row is the same length and contains all necessary characters including commas where you want to put the cell boundaries. dlmwrite() is not able to handle cell arrays.
dlmwrite() never appends to the end of an existing line. The -append flag causes it to add new data after all existing lines. The 'coffset' specification causes it to overwrite the file (unless -append was used) putting that many empty columns before the new data it writes.
I recommend you give up on using dlmwrite for this purpose.
You could switch to fopen/fprintf
A=[1 2 4;5 6 7;8 9 0]; B={'A';'B';'C'};
fid = fopen('DECALLFEATURES.csv', 'wt');
data_cell = [num2cell(A), B] .'; %transpose is important
fmt = [repmat('%d ', 1, size(A,2)), '%s\n'];
fprintf(fid, fmt, data_cell{:});
fclose(fid);
I do have to wonder why you are using a .csv file when you only have a single cell per line? csv does not interpret blanks as indicating the end of cells, so you are not creating a csv with 3 x 4 data, you are creating a csv with 3 x 1 data. csv expect commas to delimit cells.
  댓글 수: 10
Eliza
Eliza 2018년 2월 12일
whos table
Name Size Bytes Class Attributes
table 4x6 2860 cell
>>
whos name
Name Size Bytes Class Attributes
name 1x4 8 char
>> whos reshapedfea
Name Size Bytes Class Attributes
reshapedfea 1x21 168 double
Walter Roberson
Walter Roberson 2018년 2월 12일
You assigned a cell array to the variable name table but the code I gave needs to use the function named table.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by