How to export mulitple 1xN row matrix into the csv

2 views (last 30 days)
I have a mat file that has some variables each of size 1XN. I want to export all the variables to the csv.
Say I have 20 variables with size 1X50 in the mat file. Then the mat file has to be loaded and each variable has to be written into csv file.The generated csv file should be like:
1. Each variable name is mentioned in the first row and into separate column, like col1 = var1, col2 = var2..
2. Under each variable name should contain all the 50 data items.
I have attached the sample template of how the data should come. Thanks.

Answers (1)

KL
KL on 5 Nov 2017
Edited: KL on 5 Nov 2017
Just create a table and use write table,
t = array2table([A.', B.']);
writetable(t,filename);
or concatenate them and use csvwrite or dlmwrite as shown here: https://de.mathworks.com/matlabcentral/answers/259937-csvwrite-a-matrix-with-header,
varNames = {'A','B'};
fid = fopen(filename,'w');
fprintf(fid,'%s\n',varNames)
fclose(fid)
dlmwrite(filename,[A.', B.'],'-append');

Products

Community Treasure Hunt

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

Start Hunting!