Write a cell array which contains strings to a csv file

3 Ansichten (letzte 30 Tage)
Vinod
Vinod am 23 Mär. 2016
Beantwortet: Walter Roberson am 23 Mär. 2016
I am trying to write a single precision floating point array to a csv file.
csvwrite('abc.csv',num2hex(single(magic(4))))
After each character, comma appears.
How to write a 4x4 matrix to csv file in which all elements are a single string.
The contents of the csv file looks like this:
4,1,8,0,0,0,0,0
4,0,a,0,0,0,0,0
4,1,1,0,0,0,0,0
4,0,8,0,0,0,0,0
4,0,0,0,0,0,0,0
4,1,3,0,0,0,0,0
4,0,e,0,0,0,0,0
4,1,6,0,0,0,0,0
4,0,4,0,0,0,0,0
4,1,2,0,0,0,0,0
4,0,c,0,0,0,0,0
4,1,7,0,0,0,0,0
4,1,5,0,0,0,0,0
4,1,0,0,0,0,0,0
4,1,4,0,0,0,0,0
3,f,8,0,0,0,0,0
I don't want any commas in between the string.

Antworten (1)

Walter Roberson
Walter Roberson am 23 Mär. 2016
You cannot use csvwrite() for this. You will need to either use xlswrite() or write the file yourself.
fid = fopen('abc.csv', 'wt');
fmt = [repmat('%s,', 1, 3), '%s\n'];
datacell = arrayfun(@(x) num2hex(x), single(magic(4)), 'Uniform', 0);
fprintf(fid, fmt, datacell .'); %transpose is needed
fclose(fid)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by