How to write string in .CSV format?
78 views (last 30 days)
Show older comments
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
Chris
on 30 May 2022
Edited: Chris
on 30 May 2022
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]
more_correct = A + "," + B + "," + e + "," + Name
or...
number_strings = string([A;B;e]);
another_way = join([number_strings;Name], ",")
Answers (1)
dpb
on 30 May 2022
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
dpb
on 31 May 2022
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.
Walter Roberson
on 31 May 2022
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.
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!