Clear Filters
Clear Filters

Convert matrices to .csv from a double matrix and header cell

15 views (last 30 days)
Hi
I would like to put headers into a csv file i've converted. So I have a (59, 26896) double matrix which i converted to csv. However I also have a cell matrix (1,26896) header file that i would like to include. This is what I've tried:
csvwrite('result.csv', result);
commaHeader = [header;repmat({','},1,numel(header))]; %insert commaas
commaHeader = commaHeader(:)';
textHeader = cell2mat(commaHeader); %cHeader in text with commas
%write header to file
fid = fopen('result.csv','w');
fprintf(fid,'%s\n',textHeader)
fclose(fid)
%write data to end of file
dlmwrite('result.csv',result,'-append');
I gives me a totally wrong output of (59, 60353) instead of a (60, 26896) i would expect. Any help would be greatly appreciated. Thank you.
Kevin

Accepted Answer

Guillaume
Guillaume on 14 Dec 2018
The way you construct your header is way overcomplicated:
textHeader = strjoin(header, ',');
fid = fopen('result.csv', 'w');
fprintf(fid, '%s\n', textHeader);
Where does it gives me an output of 59x60353 comes from. What you're doing is writing a header (of possibly 60353 characters) on the first line, followed by a 60x26896 array of numbers in a text file. It does not give any output. Of course, you can't read that header back with csvread or dlmread. That's not supported with either function. So, if your output of 59x60353 is what you get when you read the file back, then yes of course, you haven't skipped the header.
If the name of the columns in your header can all be valid variable names, then the easiest to have them survive the round trip would be to use writetable and readtable:
t = array2table(result, 'VariableNames', header); %will error if any of the name in header is not a valid variable name
writetable(t, 'result.csv');
%reading back with header.
t = readtable('result.csv')
  2 Comments
Guillaume
Guillaume on 17 Dec 2018
Kevin Teh's comment mistakenly posted as an answer moved here:
Hi
I get error
Error using matlab.internal.tabular.private.varNamesDim.makeValidName (line 433)
'atlas.FP r (Frontal Pole Right) vs atlas.FP r (Frontal Pole Right)' is not a valid variable name.
What is a good variable and how many characters does it take? Should i do something like
atlas.FP_r_VS_atlas.FP_l? However some are very much longer than this example. Thank you for your help.
Kevin
Guillaume
Guillaume on 17 Dec 2018
As I wrote as a comment to the code: will error if any of the name in header is not a valid variable name. 'atlas.FP r (Frontal Pole Right) vs atlas.FP r (Frontal Pole Right)' is certainly not a valid variable names (dots, brackets and spaces are not allowed).
If you're happy with the header names to be slightly modified (spaces and dots will be replaced by _, other invalid characters may get removed or replaced by something else), then:
t = array2table(result, 'VariableNames', matlab.lang.makeValidName(header));
Otherwise, you cannot use table and have to use fprintf and textscan to read and write your header, respectively.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!