I have a matrix A in MATLAB that I would like to export to Microsoft Word as a table.
MATLAB: How to export a MATLAB matrix ‘A’ as a table in Microsoft Word
MATLABMATLAB Report Generatormspublishtableword
Related Solutions
One approach to solve this issue is to use categorical variables, which will allow you to identify the frequencies of each entries and then logical index to create the subset tables. This workflow is demonstrated in the code below:
% Convert Var1 to categorical data
T = convertvars(T, "Var1", 'categorical');% Set up variables for table generation
counts = countcats(T.Var1);cats = categories(T.Var1);uniqueCounts = unique(counts);% Create tables
splitTables = cell(length(uniqueCounts));for i = 1:length(uniqueCounts) currentCountCats = cats(counts == i); splitTables{i} = T(ismember(T.Var1, currentCountCats), :);end
This results in the following tables:
>> splitTables{1,1}ans = 2×2 table Var1 Var2 ____ _______ A "data1" B "data2">> splitTables{2,1}ans = 2×2 table Var1 Var2 ____ _______ C "data3" C "data4">> splitTables{3,1}ans = 3×2 table Var1 Var2 ____ _______ D "data5" D "data6" D "data7"
First, you can make a variable categorical when creating the initial table by using 'categorical' or, for an existing table, you can convert the variable to categorical using 'convertvars'. Documentation for each function can be found at the links below:
Once the entries are in a categorical variable, you can use 'categories' to get a list of the categories and 'countcats' to obtain the frequency of each category. Please see the following links for more information on these functions:
Finally, you can use logical indexing to find the categories of a certain frequency and pull the relevant rows from the original table using 'ismember'. More information on logical indexing and 'ismember' can be found here:
Note that the same process can be followed for numeric variables as well.
To display a table in a figure, see the following MATLAB Answers post, which outlines two methods, one using "uitables" and another printing the Command Window output as an "annotation" with TeX Markup on the figure:
<https://www.mathworks.com/matlabcentral/answers/254690-how-can-i-display-a-matlab-table-in-a-figure>
If you then want to make the data in the table itself appear bold (not just the columns header), try the following for the two methods described in the above link:
1) With uitables, you can format individual cells using "uistyle" objects, then add that format to the table with the "addStyle" function:
<https://www.mathworks.com/help/matlab/ref/uistyle.html?s_tid=doc_ta> <https://www.mathworks.com/help/matlab/ref/matlab.ui.control.tableappd.addstyle.html?s_tid=doc_ta>
2) If you are using an annotation with TeX Markup to display the Command Window output of the table, add your "\bf" and "\rm" tags to the beginning and end of your text, respectively. This will make all text appear bold. Below I have edited the code block from the MATLAB Answers post linked above,
% Create table
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
T = table(Age,Height,Weight,'RowNames',LastName);
% Get the table in string form.
TString = evalc('disp(T)');
% Use TeX Markup for bold formatting and underscores.
TString = strrep(TString,'<strong>',''); %these tags are now redundant
TString = strrep(TString,'</strong>',''); %these tags are now redundant
TString = strrep(TString,'_','\_');
TString = ['\bf' TString '\rm']; %add bold markup tags to beginning and end of text
% Get a fixed-width font.
FixedWidth = get(0,'FixedWidthFontName');
% Output the table using the annotation command.
annotation(gcf,'Textbox','String',TString,'Interpreter','Tex',...
'FontName',FixedWidth,'Units','Normalized','Position',[0 0 1 1]);
Best Answer