How to write string in .CSV format?

A=1;
B=2;
e=4;
Name='data stored';
Input_data=[A B e Name]
dlmwrite('input.csv',Input_data,'append')
I want to write string in dlmwrite format is it possible? 2018a version end

1 Comment

You could use
sprintf('%s,',Input_data)
but Input_data isn't going to be what you want, since you are trying to concatenate doubles with a char vector.
A=1;
B=2; e=4; Name='data stored'; Input_data=[A B e Name]
Input_data = 'data stored'
more_correct = A + "," + B + "," + e + "," + Name
more_corect = "1,2,4,data stored"
or...
number_strings = string([A;B;e]);
another_way = join([number_strings;Name], ",")
another_way = "1,2,4,data stored"

Sign in to comment.

Answers (1)

A=1;
B=2;
e=4;
Name='data stored';
Input_data={A B e Name};
writecell(Input_data,'input.csv','Delimiter',',','QuoteStrings',1,"WriteMode","append")
results in
>> type input.csv
1,2,4,"data stored"
>>

8 Comments

Right, dlmwrite is the wrong tool for this job. Even if the matlab version is too early to have writecell, dlmwrite is the wrong tool for this task.
If that were the case, one could always convert to a table and call writetable instead. That goes back another 5 year or so...
Rakesh Roshan
Rakesh Roshan on 31 May 2022
Edited: Rakesh Roshan on 31 May 2022
Sir my version is 2018a writecell is not there
writetable() with 'writevariablenames', false and specify a .csv file.
dpb
dpb on 31 May 2022
Edited: dpb on 31 May 2022
writecell is just a wrapper for writetable anyways...just saves the user the convert to table step for the inputs -- but it does cell2table inside.
is it not possibe with only dlmwrite?
No. dlmwrite (and its companion csvwrite) handle only numeric data in an array as documented; they "know nuthink!" about cell arrays nor string data; you would have to convert the string data to numeric ASCII code and catenate to the numeric values into a single array -- ok, you could write each to a record with the 'append' option, but that would entail opening/closing the file for every element; brute ugly and still the character data would have to be written as ASCII codes.
MATLAB historically has been very weak in output for the cell array and cell string; one generally had to resort to the low-level i/o with fprintf until the recent introduction of these newer functions.
Yes, it is possible with dlmwrite. What you need to do is convert everything you need to write into a cell array of character vectors, one per row, such as
Input_data{1} = sprintf( '%g,%g,%g,"%s"', A, B, e, Name);
Note that this required coding all of commas and double-quotes yourself.
Then,
dlmwrite(FILENAME, Input_data{1}, 'delimiter', '', 'precision', '%s') ;
Now loop over the remaining entries in the cell Input_data, doing a separate dlmwrite call for each one, like the above but adding also 'Writemode', 'append' to the list of options.
What this code would be doing is opening the output file for each row, writing out one row, closing the file, and then looping back for the next row. The array of data you would be passing to dlmwrite would be the individual character vector that was the already-formatted text for the line. Remember that a character vector is an array of characters, so as far as dlmwrite is concerned it would be writing out as many columns as there were characters in the vector. The 'Delimiter', '' would tell dlmwrite to skip the normal comma between columns, and the 'precision', '%s' tells it to write each entry in the vector as a character.
This is all very ugly and inefficient, and the only reason to do this is to prove that it can be done. If you were going to preformat the lines anyhow then a single fopen(), fprintf(), fclose() would be much more efficient. And if you use fopen(), loop the fprintf, then fclose() you can format as you go.

Sign in to comment.

Tags

Asked:

on 30 May 2022

Commented:

on 31 May 2022

Community Treasure Hunt

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

Start Hunting!