フィルターのクリア

Printing output of fitglm as table to file

29 ビュー (過去 30 日間)
Jacqueline Scholl
Jacqueline Scholl 2019 年 8 月 1 日
コメント済み: Elsa Fouragnan 2024 年 3 月 6 日
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
  5 件のコメント
Jacqueline Scholl
Jacqueline Scholl 2019 年 8 月 2 日
wow, thank you so much! that would have taken me forever to figure out!
Adam Danz
Adam Danz 2019 年 8 月 2 日
編集済み: Adam Danz 2019 年 8 月 2 日
Glad I could help!

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 8 月 1 日
編集済み: Adam Danz 2022 年 6 月 21 日
Update: MATLAB R2021a or later
Use the formattedDisplayText function to capture the model output as a string (see Community Highlight).
%% 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 = formattedDisplayText(mdl)
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
struct2str = @(x)strsplit(evalc('disp(x);'),newline)';
txt{6,1} = ['________R-Squared________'; struct2str(mdl.Rsquared)];
txt{7,1} = ['_____Criterion_____'; struct2str(mdl.ModelCriterion)];
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
(Updated on 6/21/22 to define struct2str)
Here's an screen shot of the resultant text file.

その他の回答 (2 件)

Vlad Pineta
Vlad Pineta 2022 年 6 月 20 日
I've created the same file but exporting as LaTex and without the error about "struct2str"
function regressionExport(name,mdl,vargin)
txt{1,1} = '\begin{table}[]';
txt{2,1} = '\centering';
txt{3,1} = '\begin{tabular}{|ccccccc}';
txt{4,1} = '\hline';
txt{5,1} = strcat('\multicolumn{7}{|c|}{',mdl.Formula.char,'}',{' '},'\\ \hline');
txt{6,1} = strcat('\multicolumn{7}{|c|}{','Distribution =',{' '},mdl.Distribution.Name,'}',' \\ \hline');
tabel_coefficients = [mdl.Coefficients, array2table(mdl.coefCI,'VariableNames',{'coefCIlow','coefCIhigh'})];
tabel_coefficients_cname = [' ', tabel_coefficients.Properties.VariableNames];
tabel_coefficients_rname = tabel_coefficients.Properties.RowNames;
tabel_coefficients_datastr = cellfun(@num2str,table2cell(tabel_coefficients),'UniformOutput',false);
tabel_coefficients = [tabel_coefficients_cname; [tabel_coefficients_rname, tabel_coefficients_datastr]];
tabel_coefficients = string(tabel_coefficients);
txt{7,1} = strcat(tabel_coefficients{1,1},{' '},'&',{' '},tabel_coefficients{1,2},{' '},'&',{' '},tabel_coefficients{1,3},{' '},'&',{' '},tabel_coefficients{1,4},{' '},'&',{' '},tabel_coefficients{1,5},{' '},'&',{' '},tabel_coefficients{1,6},{' '},'& \multicolumn{1}{c|}{',tabel_coefficients{1,7},'} \\ \cline{2-7}');
tabel_coefficients(1,:) = [];
txt8 = {};
for i = 1:size(tabel_coefficients,1)
if i == size(tabel_coefficients,1)
x = strcat('\multicolumn{1}{|c|}{',tabel_coefficients(i,1),'} &',{' '},tabel_coefficients(i,2),{' '},'&',{' '},tabel_coefficients(i,3),{' '},'&',{' '},tabel_coefficients(i,4),{' '},'&',{' '},tabel_coefficients(i,5),{' '},'&',{' '},tabel_coefficients(i,6),{' '},'& \multicolumn{1}{c|}{',tabel_coefficients(i,7),'} \\ \hline');
txt8 = [txt8;x];
else
x = strcat('\multicolumn{1}{|c|}{',tabel_coefficients(i,1),'} &',{' '},tabel_coefficients(i,2),{' '},'&',{' '},tabel_coefficients(i,3),{' '},'&',{' '},tabel_coefficients(i,4),{' '},'&',{' '},tabel_coefficients(i,5),{' '},'&',{' '},tabel_coefficients(i,6),{' '},'& \multicolumn{1}{c|}{',tabel_coefficients(i,7),'} \\');
txt8 = [txt8;x];
end
end
txt{8,1} = txt8;
txt{9,1} = strcat('\multicolumn{7}{|c|}{',string(mdl.NumObservations),' observations ,',{' '},string(mdl.DFE),' df','} \\ \hline');
[pv,fstat] = coefTest(mdl);
txt{10,1} = strcat('\multicolumn{7}{|c|}{',sprintf('F-Statistic vs. constant model: %.1f, p = %0.6f', fstat, pv),'} \\ \hline');
txt{11,1} = strcat('\multicolumn{2}{|c|}{R-Squared} & & & & & \\ \cline{1-2}');
tabel_rsquared = struct2table(mdl.Rsquared);
cell_rsquared = table2cell(tabel_rsquared);
titles_rsquared = tabel_rsquared.Properties.VariableNames;
cell_rsquared = [titles_rsquared;cell_rsquared];
cell_rsquared = cell_rsquared';
cell_rsquared = string(cell_rsquared);
txt12 = {};
for i = 1:size(cell_rsquared,1)
if i == size(cell_rsquared,1)
x = strcat(cell_rsquared(i,1),{' '},'& \multicolumn{1}{c|}{',cell_rsquared(i,2),'} & & & & & \\ \cline{1-2}');
txt12 = [txt12;x];
else
x = strcat(cell_rsquared(i,1),{' '},'& \multicolumn{1}{c|}{',cell_rsquared(i,2),'} & & & & & \\');
txt12 = [txt12;x];
end
end
txt{12,1} = txt12;
txt{13,1} = strcat('\multicolumn{2}{|c|}{Criterion} & & & & & \\ \cline{1-2}');
tabel_modelcriterion = struct2table(mdl.ModelCriterion);
cell_modelcriterion = table2cell(tabel_modelcriterion);
titles_modelcriterion = tabel_modelcriterion.Properties.VariableNames;
cell_modelcriterion = [titles_modelcriterion;cell_modelcriterion];
cell_modelcriterion = cell_modelcriterion';
cell_modelcriterion = string(cell_modelcriterion);
txt14 = {};
for i = 1:size(cell_modelcriterion,1)
if i == size(cell_modelcriterion,1)
x = strcat(cell_modelcriterion(i,1),{' '},'& \multicolumn{1}{c|}{',cell_modelcriterion(i,2),'} & & & & & \\ \cline{1-3}');
txt14 = [txt14;x];
else
x = strcat(cell_modelcriterion(i,1),{' '},'& \multicolumn{1}{c|}{',cell_modelcriterion(i,2),'} & & & & & \\');
txt14 = [txt14;x];
end
end
txt{14,1} = txt14;
tabel_variableinfo = mdl.VariableInfo;
tabel_variableinfo.Class = [];
tabel_variableinfo.Range = [];
tabel_variableinfo_cname = [' ', tabel_variableinfo.Properties.VariableNames];
tabel_variableinfo_rname = tabel_variableinfo.Properties.RowNames;
tabel_variableinfo_datastr = cellfun(@num2str,table2cell(tabel_variableinfo),'UniformOutput',false);
tabel_variableinfo = [tabel_variableinfo_cname; [tabel_variableinfo_rname, tabel_variableinfo_datastr]];
tabel_variableinfo = string(tabel_variableinfo);
txt{15,1} = strcat(tabel_variableinfo{1,1},{' '},'&',{' '},tabel_variableinfo{1,2},{' '},'& \multicolumn{1}{c|}{',tabel_variableinfo{1,3},'} & & & \multicolumn{1}{l}{} & \\ \cline{2-3}');
tabel_variableinfo(1,:) = [];
txt16 = {};
for i = 1:size(tabel_variableinfo,1)
if i == size(tabel_variableinfo,1)
x = strcat('\multicolumn{1}{|c|}{',tabel_variableinfo(i,1),'} &',{' '},tabel_variableinfo(i,2),{' '},'& \multicolumn{1}{c|}{',tabel_variableinfo(i,3),'} & & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \\ \cline{1-3}');
txt16 = [txt16;x];
else
x = strcat('\multicolumn{1}{|c|}{',tabel_variableinfo(i,1),'} &',{' '},tabel_variableinfo(i,2),{' '},'& \multicolumn{1}{c|}{',tabel_variableinfo(i,3),'} & & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \\');
txt16 = [txt16;x];
end
end
txt{16,1} = txt16;
txt{17,1} = '\end{tabular}';
txt{18,1} = '\end{table}';
notCellIdx = ~cellfun(@iscell, txt);
txt(notCellIdx) = cellfun(@(x){{x}},txt(notCellIdx));
txt = vertcat(txt{:});
fid = fopen(strcat(name,'.txt'), 'wt');
fprintf(fid,'%s\n', txt{:});
fclose(fid);
end
I know it's not the best code but it works :)
  3 件のコメント
Vlad Pineta
Vlad Pineta 2022 年 6 月 21 日
I've modified the code to be able to print the results as pdf and i removed the inModel and isCategorical fields for the covariance coefficient. Also i rearranged the R-squared and Criterion fields for a nicer view.
function export = fitlmExport(rEdataFolder,rEname,mdl,vargin)
optionExport = 1;
if optionExport == 1
txt{1,1} = {'\documentclass[convert]{standalone}';
'\begin{document}';
'\begin{minipage}{1.6\textwidth}'};
end
txt{2,1} = '\begin{table}[!htb]';
txt{3,1} = '\centering';
txt{4,1} = '\begin{tabular}{|ccccccc|}';
txt{5,1} = '\hline';
txt{6,1} = strcat('\multicolumn{7}{|c|}{',rEname,'}',{' '},'\\ \hline');
txt{7,1} = strcat('\multicolumn{7}{|c|}{',mdl.Formula.char,'}',{' '},'\\ \hline');
tabel_coefficients = [mdl.Coefficients, array2table(mdl.coefCI,'VariableNames',{'coefCIlow','coefCIhigh'})];
tabel_coefficients_cname = [' ', tabel_coefficients.Properties.VariableNames];
tabel_coefficients_rname = tabel_coefficients.Properties.RowNames;
tabel_coefficients_datastr = cellfun(@num2str,table2cell(tabel_coefficients),'UniformOutput',false);
tabel_coefficients = [tabel_coefficients_cname; [tabel_coefficients_rname, tabel_coefficients_datastr]];
tabel_coefficients = string(tabel_coefficients);
txt{8,1} = strcat('\multicolumn{1}{|c}{}',tabel_coefficients{1,1},{' '},'&',{' '},tabel_coefficients{1,2},{' '},'&',{' '},tabel_coefficients{1,3},{' '},'&',{' '},tabel_coefficients{1,4},{' '},'&',{' '},tabel_coefficients{1,5},{' '},'&',{' '},tabel_coefficients{1,6},{' '},'&',{' '},tabel_coefficients{1,7},{' '},'\\ \cline{2-7}');
tabel_coefficients(1,:) = [];
txt9 = {};
for i = 1:size(tabel_coefficients,1)
if i == size(tabel_coefficients,1)
x = strcat('\multicolumn{1}{|c|}{',tabel_coefficients(i,1),'} &',{' '},tabel_coefficients(i,2),{' '},'&',{' '},tabel_coefficients(i,3),{' '},'&',{' '},tabel_coefficients(i,4),{' '},'&',{' '},tabel_coefficients(i,5),{' '},'&',{' '},tabel_coefficients(i,6),{' '},'&',{' '},tabel_coefficients(i,7),{' '},'\\ \hline');
txt9 = [txt9;x];
else
x = strcat('\multicolumn{1}{|c|}{',tabel_coefficients(i,1),'} &',{' '},tabel_coefficients(i,2),{' '},'&',{' '},tabel_coefficients(i,3),{' '},'&',{' '},tabel_coefficients(i,4),{' '},'&',{' '},tabel_coefficients(i,5),{' '},'&',{' '},tabel_coefficients(i,6),{' '},'&',{' '},tabel_coefficients(i,7),{' '},'\\');
txt9 = [txt9;x];
end
end
txt{9,1} = txt9;
txt{10,1} = strcat('\multicolumn{7}{|c|}{',string(mdl.NumObservations),' observations ,',{' '},string(mdl.DFE),' df','} \\ \hline');
[pv,fstat] = coefTest(mdl);
txt{11,1} = strcat('\multicolumn{7}{|c|}{',sprintf('F-Statistic vs. constant model: %.1f, p = %0.6f', fstat, pv),'} \\ \hline');
txt{12,1} = '\multicolumn{7}{l}{} \\ \cline{2-7} ';
tabel_rsquared = struct2table(mdl.Rsquared);
cell_rsquared = table2cell(tabel_rsquared);
titles_rsquared = tabel_rsquared.Properties.VariableNames;
cell_rsquared = [titles_rsquared;cell_rsquared];
cell26 = repmat({''}, 2, 6);
cell_rsquared = [cell_rsquared,cell26];
cell_rsquared = string(cell_rsquared);
txt{13,1} = strcat('\multicolumn{1}{c|}{} &',{' '},cell_rsquared(1,1),{' '},'&',{' '},cell_rsquared(1,2),{' '},'&',{' '},cell_rsquared(1,3),{' '},'&',{' '}',cell_rsquared(1,4),{' '},'&',{' '},cell_rsquared(1,5),{' '},'&',{' '},cell_rsquared(1,6),{' '},'\\ \cline{1-1}');
txt{14,1} = strcat('\multicolumn{1}{|c}{R-Squared} &',{' '},cell_rsquared(2,1),{' '},'&',{' '},cell_rsquared(2,2),{' '},'&',{' '},cell_rsquared(2,3),{' '},'&',{' '},cell_rsquared(2,4),{' '},'&',{' '},cell_rsquared(2,5),{' '},'&',{' '},cell_rsquared(2,6),{' '},'\\ \hline');
tabel_modelcriterion = struct2table(mdl.ModelCriterion);
cell_modelcriterion = table2cell(tabel_modelcriterion);
titles_modelcriterion = tabel_modelcriterion.Properties.VariableNames;
cell_modelcriterion = [titles_modelcriterion;cell_modelcriterion];
cell26 = repmat({''}, 2, 6);
cell_modelcriterion = [cell_modelcriterion,cell26];
cell_modelcriterion = string(cell_modelcriterion);
txt{15,1} = strcat('\multicolumn{1}{c|}{} &',cell_modelcriterion(1,1),{' '},'&',{' '},cell_modelcriterion(1,2),{' '},'&',{' '},cell_modelcriterion(1,3),{' '},'&',{' '},cell_modelcriterion(1,4),{' '},'&',{' '},cell_modelcriterion(1,5),{' '},'&',{' '},cell_modelcriterion(1,6),{' '},'\\ \cline{1-1}');
txt{16,1} = strcat('\multicolumn{1}{|c}{Criterion} &',{' '},cell_modelcriterion(2,1),{' '},'&',{' '},cell_modelcriterion(2,2),{' '},'&',{' '},cell_modelcriterion(2,3),{' '},'&',{' '},cell_modelcriterion(2,4),{' '},'&',{' '},cell_modelcriterion(2,5),{' '},'&',{' '},cell_modelcriterion(2,6),{' '},'\\ \hline');
txt{17,1} = '\end{tabular}';
txt{18,1} = '\end{table}';
cell_CoefficientCovariance = num2cell(mdl.CoefficientCovariance);
cell_CoefficientNames = mdl.CoefficientNames;
cell_CoefficientCovariance = [cell_CoefficientNames;cell_CoefficientCovariance];
cell_CoefficientNames = ['Coefficient Covariance',cell_CoefficientNames];
cell_CoefficientNames = cell_CoefficientNames';
cell_CoefficientCovariance = [cell_CoefficientNames,cell_CoefficientCovariance];
cell_CoefficientCovariance = string(cell_CoefficientCovariance);
txt{19,1} = '\begin{table}[]';
txt{20,1} = '\centering';
txt{21,1} = strcat('\begin{tabular}{|',repelem('c',size(cell_CoefficientCovariance,1)),'|}');
txt{22,1} = '\hline';
txt23 = {};
txt23_row = {};
for j = 1:size(cell_CoefficientCovariance,1)
for i = 1:size(cell_CoefficientCovariance,2)
if i == 1 && j == 1
temp_txt23 = strcat({' '},cell_CoefficientCovariance(j,i),{' '},'&');
elseif i == size(cell_CoefficientCovariance,2) && j == 1
temp_txt23 = strcat({' '},cell_CoefficientCovariance(j,i),{' '},'\\ \cline{2-',string(size(cell_CoefficientCovariance,2)),'}');
elseif i == 1 && j ~= 1
temp_txt23 = strcat('\multicolumn{1}{|c|}{',cell_CoefficientCovariance(j,i),'}',{' '},'&');
elseif i == size(cell_CoefficientCovariance,2) && j ~= size(cell_CoefficientCovariance,1)
temp_txt23 = strcat({' '},cell_CoefficientCovariance(j,i),{' '},'\\');
elseif i == size(cell_CoefficientCovariance,2) && j == size(cell_CoefficientCovariance,1)
temp_txt23 = strcat({' '},cell_CoefficientCovariance(j,i),{' '},'\\ \hline');
else
temp_txt23 = strcat({' '},cell_CoefficientCovariance(j,i),{' '},'&');
end
txt23_row = [txt23_row,temp_txt23];
end
txt23 = [txt23;txt23_row];
txt23_row = {};
end
txt23=cellfun(@string,txt23);
txt{23,1} = join(txt23);
txt{24,1} = '\end{tabular}';
txt{25,1} = '\end{table}';
if optionExport == 1
txt{26,1} = {'\end{minipage}';
'\end{document}'};
end
for i = 1:size(txt,1)
txt{i,1} = regexprep(txt{i,1},'([$_^])','\.');
end
notCellIdx = ~cellfun(@iscell, txt);
txt(notCellIdx) = cellfun(@(x){{x}},txt(notCellIdx));
txt = vertcat(txt{:});
export = sprintf('%s\n', txt{:});
if optionExport == 1
fid = fopen(strcat(rEdataFolder,rEname,'.txt'), 'wt');
fprintf(fid,'%s\n', txt{:});
fclose(fid);
fid = fopen(strcat(rEdataFolder,rEname,'.tex'), 'wt');
fprintf(fid,'%s\n', txt{:});
fclose(fid);
cmd = ['cd ' rEdataFolder ' && latex ' rEname '.tex && dvipdfm ' rEname ' && ' rEname '.pdf'];
system(cmd);
end
end
Adam Danz
Adam Danz 2022 年 6 月 21 日
編集済み: Adam Danz 2022 年 6 月 21 日
It's probably a lot easier to just use formattedDisplayText which was not available when my answer was originally added.

サインインしてコメントする。


Marina Fernandez
Marina Fernandez 2022 年 9 月 21 日
Another option is to convert the model to text, separate it by rows and convert that to a cell in order to write the results of the model in excel format:
text_model = evalc('disp(model)');
split_text_model = split(text_model,char(10));
xlswrite(excel_path,cellstr(split_text_model),'results','A1');
  1 件のコメント
Elsa Fouragnan
Elsa Fouragnan 2024 年 3 月 6 日
this doesn't work on my end.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by