how can I append this format using dlmwrite ?

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');

Answers (1)

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 Comments

Thanks sir ,I am working on your helpful answer to modify the code .Actually I am working on creating my own data set. this portion of code is simplified version to help in understanding the question.Your wondering in the end was very helpful because I did not notice that.I thought it will work like text file as I was working earlier.
You might want to consider building your data as a table.
>> t = table(char('A' + randi(25,10,2)), randi(25,10,1));
>> writetable(t, 'test.csv','writevar',false, 'delimiter',' ')
>> !cat test.csv
IL 17
GG 2
CY 6
WK 21
HQ 12
VD 4
GB 9
VY 20
EX 19
LN 12
Here is some values of one row of the real data
10596,10757,10979,11258,11549,11798 0101
I generate these values in the loop the last value 0101 is the target all previous values are features I wanna store them instead of text file in .csv file. the target is stored as char when I used the previous code here is the error
Subscript indices must either be real positive integers or logicals.
I am not sure which previous code you used?
t = table(char('A' + randi(25,10,2)), randi(25,10,1));
>> writetable(t, 'test.csv','writevar',false, 'delimiter',' ')
>> !cat test.csv
Which line is producing the error about subscripts, and how does this relate to your actual data, and what variable names did you use?
answer Q1 :The line include the error is this one
t = table(reshapedfea,char(fullfile(name)));
answer Q2: It relate to my actual data because it is double. The error said that the subscript require real positive integer or Logical.
answer Q3:
t = table(reshapedfea,char(fullfile(name)));
writetable(t, 'test.csv','writevar',false, 'delimiter',' ')
reshapedfae is the features that I wanna make the rows of the table here is some values of these features
10596,10757,10979,11258,11549,11798
fullfile(name) is the label which is I want it at the last or beginning of the feature row it is not numeric value here is the example of the value
0101
Please do not use fullfile() as the name of a variable. fullfile() is the name of a useful utility routine that is intended to take at least two parameters and to create a path name by putting the inputs together with a system-appropriate path specification.
You should use
which name
which fullfile
which char
which table
to determine which of those are numeric values that might not be positive real-valued integers.
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
You assigned a cell array to the variable name table but the code I gave needs to use the function named table.

Sign in to comment.

Asked:

on 9 Feb 2018

Commented:

on 12 Feb 2018

Community Treasure Hunt

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

Start Hunting!