Table header - possible to use special characters?

99 views (last 30 days)
I have constructed a table with data from a struct and now wish to add custom headers to the columns before exporting the table. I found the following command:
T.Properties.VariableNames{'OldHeader'} = 'NewHeader';
This command however does not allow me to use spaces or special characters for my headers. My table contains the output from processed lab data and I wish to have headers like this:
"Vol. [mL]" and "Conc. [wt%]"
Is this in any way possible?

Answers (2)

Andrew Reibold
Andrew Reibold on 14 Nov 2014
I don't know if it actually works: Try using sprintf to print what text you want
  2 Comments
Andrew Reibold
Andrew Reibold on 14 Nov 2014
So I had a spare second and just tried to establish "Vol mL" as a variable name not even include the special characters, and it says it was not an allowable variable name (Just for the space!)
I then tried to establish just "Vol[mL]" and was denied as well (Just for a special character!)
What a pain! I see what your problem is! My solution will not work and you will probably need help from one of the nerds (A term of endearment) on here that know how to alter the underlying Java if possible.
If you are able to avoid tables and use matrices, I would recommend it after seeing how inflexible they can be.

Sign in to comment.


Guillaume
Guillaume on 14 Nov 2014
As detailed in the documentation of table You can only use valid Matlab variable names for your table variable names. It can only contain letters, numbers or underscore. One would assume that by letters mathworks means A-Za-z.
So spaces or symbols are not allowed.
Java won't help you there. It's intrinsic to matlab.
  2 Comments
Guillaume
Guillaume on 14 Nov 2014
Matlab's high level functions are not particularly good for writing headers to files. You could use xlswrite assuming you have excel installed. Otherwise, you have to use low level functions:
fid = fopen('somefile.txt', 'wt');
fprintf(fid, 'Vol. (ml)\tConc. [wt%]\n');
for row = 1:size(m, 1)
fprintf(fid, '%f\t%f\n', m(row, 1), m(row, 2));
end
fclose(fid);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!