Clear Filters
Clear Filters

Table header - possible to use special characters?

69 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 (3)

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
Morten Jensen
Morten Jensen on 14 Nov 2014
one of the columns would be
sprintf('Vol. [mL]')
which I can't seem to get to work as a header.
Taking mathworks own example from struct2table documentation :
S.Name = {'CLARK';'BROWN';'MARTIN'};
S.Gender = {'M';'F';'M'};
S.SystolicBP = [124;122;130];
S.DiastolicBP = [93;80;92];
T = struct2table(S)
T.Properties.VariableNames{'Gender'} = 'Sex';
The above works, but restricts me to normal characters and no spaces. My question is how to change "Gender" to "Vol. [mL]" - if even possible.
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
Morten Jensen
Morten Jensen on 14 Nov 2014
then is there a workaround way to achieve the desired result? I've tried to set up the data in a cell array but having trouble exporting the data due to the limitations of csvwrite
The exported data is to be printed and it is therefore needed to have some clearly defined headers and not just variable names
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.


Brandon Ballard
Brandon Ballard on 19 Jul 2023
Edited: Brandon Ballard on 19 Jul 2023
After utilising ASCII codes for the special characters it is possible to use them in the creation of custom headers.
for example:
double(' ')
ans = 32
double('[')
ans = 91
double(']')
ans = 93
double('.')
ans = 46
so for the above example your code would need to be constructed as follows:
T = table([0:4],[1:9],[3:7],'VariableNames',{'x','y','OldHeader'})
T = 1×3 table
x y OldHeader _____________________ __________ _____________________ 0 1 2 3 4 1×9 double 3 4 5 6 7
T.Properties.VariableNames{'OldHeader'} = ['Vol',char(46),char(32),char(91),'mL',char(93)];
disp(T)
x y Vol. [mL] _____________________ __________ _____________________ 0 1 2 3 4 1×9 double 3 4 5 6 7
This works if all you want is to visualise the data in the command window but could result in issues if trying to refer to the value using standard dot notation.
To compensate for this the collumn can be accessed using:
T{1,3} % Column Number corresponds to column being chosen.
ans = 1×5
3 4 5 6 7
This approach is a workaround the current methodology and may not be supported by other functions but is useful for creating a display for the table.
I have tried a number of other ASCII values and you are able to have blank headers with the space character if using the char(32) as the variable name.
__________________________________________________________________________________________
The following code was utilised in Matlab 2023a, other versions of Matlab may not offer the same functionality and may result in unexpected results. The Author of this answer is not liable for any injuries/accidents caused through the use of this code and offers no guarantees or warranty. Use of the code is at own risk.
  2 Comments
Stephen23
Stephen23 on 19 Jul 2023
Edited: Stephen23 on 19 Jul 2023
"After utilising ASCII codes for the special characters it is possible to use them in the creation of custom headers."
It is definitely not required to specify the character codes like that:
T = table([0:4],[1:9],[3:7],'VariableNames',{'x','y','OldHeader'})
T = 1×3 table
x y OldHeader _____________________ __________ _____________________ 0 1 2 3 4 1×9 double 3 4 5 6 7
T.Properties.VariableNames{'OldHeader'} = 'Vol. [mL] яäœ'
T = 1×3 table
x y Vol. [mL] яäœ _____________________ __________ _____________________ 0 1 2 3 4 1×9 double 3 4 5 6 7
"... but could result in issues if trying to refer to the value using standard dot notation."
T.('Vol. [mL] яäœ')
ans = 1×5
3 4 5 6 7
The actual reason occured with R2019b and is explained here:
Note that it is recommended to use:
Brandon Ballard
Brandon Ballard on 19 Jul 2023
Edited: Brandon Ballard on 19 Jul 2023
Thank You @Stephen23 for the updated information and solution.
I had not found this documentation on my initial google search (looking at another similar topic) and saw that the question was unanswered.
I am still new to tables so this is very helpful information.
______________________
Edit:
After searching for table help directly on Google the renamevars function is obvious but the information on the help page does not include special characters in any of the examples except the space character.
I was searching for a way to display the information in a table visually in the command window with empty rows or collumns using direct conversion from a char array.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!