How Can I Append Multiple Lined Header to a CSV File?

I am looking to create a csv file with the date/time, variable names, and a table of data - something like:
Date, 01/11/20
A, B
1, 4
2, 5
3, 7
I've created a 2x2 cell for the header, and have an array of doubles containing the data. However, I cannot understand the error message I receive when trying to use the following code. What do I need to change in the code to make this work?
savetofile = 'test.csv';
date = datetime(now,'ConvertFrom','datenum');
data = [1 4; 2 5; 3 7];
data = array2table(data);
headerlines = {'Date:', date;
' A', 'B'};
fid = fopen(savetofile, 'wt');
for l = 1:numel(headerlines)
fprintf(fid,'%s\n', headerlines{l});
end
fprintf(fid, '%f; %f\n', data{:});
fclose(fid);
The error message I get is:
Subscripting into a table using one subscript (as in t(i)) or three or more subscripts (as in t(i,j,k)) is not supported. Always specify a row subscript and a variable subscript, as in t(rows,vars).
What am I doing wrong the subscripting?

 Accepted Answer

The indexing of table using data{:} is incorrect. This shows all the ways a table can be indexed: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html. However, writecell() will be a more robust solution.
savetofile = 'test.csv';
date = datetime(now,'ConvertFrom','datenum');
data = [1 4; 2 5; 3 7];
data = num2cell(data);
headerlines = {'Date:', date;
' A', 'B'};
C = [headerlines; data];
writecell(C, savetofile)

5 Comments

Thanks Ameer, the example works, but it is not working in my code unfortunately - I have framed the question wrong. The following better matches my code, and retruns the same error message - "All tables in the bracketed expression must have the same variable name" on line 16. This is a result of me merging my data arrays as in the following:
savetofile = 'test.csv';
date = datetime(now,'ConvertFrom','datenum');
dataa = [1 2 3];
datab = [4 5 7];
dataa = reshape(dataa, 3,1);
datab = reshape(datab, 3,1);
output = cat(2,dataa,datab);
output = array2table(output);
headerlines = {'Date:', date;
'A', 'B'};
headerlines = array2table(headerlines);
C = [headerlines; output];
writecell(C, savetofile)
Would you be able to advise as to what I've done wrong, and how I could fix it?
I am not sure why you want to create table in your code. You can just use cell arrays.
savetofile = 'test.csv';
date = datetime(now,'ConvertFrom','datenum');
dataa = [1 2 3];
datab = [4 5 7];
dataa = reshape(dataa, 3,1);
datab = reshape(datab, 3,1);
output = cat(2,dataa,datab);
output = num2cell(output);
headerlines = {'Date:', date;
'A', 'B'};
C = [headerlines; output];
writecell(C, savetofile)
Thank you very much Ameer - I confused the num2cell function.
I am glad to be of help!
Ted, creating a cell array is going to use a lot of memory if your data are even moderately large, and using cell arrays for numeric adata is just a bad habit that you want to stay away from. Nothing wrong with using a table. Write your header to the file, then append to it using writetable. If you have a recent version of MATLAB, use writematrix.
Also, use date = datetime('now') instead of convert from datetime.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!