Table header - possible to use special characters?
53 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
1 Kommentar
Stephen23
am 19 Jul. 2023
Note: the answers and discussion on this thread are outdated. Please read:
Antworten (3)
Andrew Reibold
am 14 Nov. 2014
I don't know if it actually works: Try using sprintf to print what text you want
2 Kommentare
Andrew Reibold
am 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.
Guillaume
am 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 Kommentare
Guillaume
am 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);
Brandon Ballard
am 19 Jul. 2023
Bearbeitet: Brandon Ballard
am 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(' ')
double('[')
double(']')
double('.')
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.Properties.VariableNames{'OldHeader'} = ['Vol',char(46),char(32),char(91),'mL',char(93)];
disp(T)
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.
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 Kommentare
Stephen23
am 19 Jul. 2023
Bearbeitet: Stephen23
am 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.Properties.VariableNames{'OldHeader'} = 'Vol. [mL] яäœ'
"... but could result in issues if trying to refer to the value using standard dot notation."
T.('Vol. [mL] яäœ')
The actual reason occured with R2019b and is explained here:
Note that it is recommended to use:
Brandon Ballard
am 19 Jul. 2023
Bearbeitet: Brandon Ballard
am 19 Jul. 2023
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.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!