MATLAB: Printing output of fitglm as table to file

fitglmfitglm publicationprint tablesave tabletext;

Dear all,
I have fitted my regression with fitglm, inputting a table. Therefore, the output looks like a nice table with named regressors etc.
What is the best way to save this as a file for publication? An html table would be good or I guess a figure would be ok too.
Many thanks
Jacquie

Best Answer

Method 1: Convert model summary to char array
%% Create a demo model

load hospital %built-in matlab data

dsa = hospital;
modelspec = 'Smoker ~ Age*Weight*Sex - Age:Weight:Sex';
mdl = fitglm(dsa,modelspec,'Distribution','binomial');
% Convert summary to char array
txt = evalc('mdl');
Method 2: Extract parts of model
This method extracts each section of text from the model and stores the text in a nx1 cell array named txt. The cell aray of text will then be written to a text file.
%% Create a demo model
load hospital %built-in matlab data
dsa = hospital;
modelspec = 'Smoker ~ Age*Weight*Sex - Age:Weight:Sex';
mdl = fitglm(dsa,modelspec,'Distribution','binomial');
%% Create text and tables
% Create rows of text that should come before the table
txt{1,1} = mdl.Formula.char;
txt{2,1} = sprintf('Distribution = %s',mdl.Distribution.Name);
% Create the table of coefficients. I also appended the confidence intervals to the end.
txt{3,1} = table2CellStrFcn([mdl.Coefficients, array2table(mdl.coefCI,'VariableNames',{'coefCIlow','coefCIhigh'})]);
% Create rows of text that should come after the table
txt{4,1} = sprintf('%d observations, %d df', mdl.NumObservations, mdl.DFE);
[pv,fstat] = coefTest(mdl);
txt{5,1} = sprintf('F-Statistic vs. constant model: %.1f, p = %0.6f', fstat, pv);
% Create additional tables if needed
txt{6,1} = ['________R-Squared________'; struct2str(mdl.Rsquared,'AlignColon', true)];
txt{7,1} = ['_____Criterion_____'; struct2str(mdl.ModelCriterion,'AlignColon', true)];
varinfoTable = mdl.VariableInfo;
varinfoTable.Class = [];
varinfoTable.Range = [];
txt{8,1} = table2CellStrFcn(varinfoTable);
%% Write text and tables to file.
% Convert any non-cell elements of txt to cell
notCellIdx = ~cellfun(@iscell, txt);
txt(notCellIdx) = cellfun(@(x){{x}},txt(notCellIdx));
% add an empty row between each element to separate the sections of text
txtSpace = repmat({{' '}}, size(txt));
txt = reshape([txt';txtSpace'],[],1);
% Vertically concatenate cell arrays
txt = vertcat(txt{:});
% Open / Create a new text file named 'GLM_results.txt'.
fid = fopen('GLM_results.txt', 'wt'); % a full path would be better
fprintf(fid,'%s\n', txt{:});
fclose(fid);
%% Function that converts table to cell of strings
function Tstr = table2CellStrFcn(T)
% Input a numeric table with column and row names (T) and convert to cell of strings (Tstr)
cname = [' ', T.Properties.VariableNames];
rname = T.Properties.RowNames;
datastr = cellfun(@num2str,table2cell(T),'UniformOutput',false);
Tstr = [cname; [rname, datastr]];
% pad elements of Tstr so columns have the same length
maxLen = max(cellfun(@numel, Tstr),[], 1);
for i = 1:size(Tstr,2)
Tstr(:,i) = pad(Tstr(:,i),maxLen(i),'right');
end
% Join columns to make nx1 array
Tstr = cellfun(@strjoin,mat2cell(Tstr,ones(1,size(Tstr,1)), size(Tstr,2)),'Unif',false);
end
Here's an screen shot of the resultant text file.