How can I add figures from a looping script in the report generator toolbox?

13 views (last 30 days)
I have a script that plots 4 different figures with each loop. When running this program from the editor, all my figures appear in the right order. However, when I am in report generator mode, the figures are placed in a .doc document randomly, even though the figures appear in the right order when the report generator runs using part of the string that generates these plots.
Here's what I have tried in the report generator tree:
Report 'mynamereport'
Title page(child from report 'mynamereport')
Chapter 1 (child from report 'mynamereport')
%Evaluate Matlab expresion (child from Chapter 1) %this is where script runs loop and plots figures
FigureLoop 1 Chapter All (sibling from Chapter 1)
Figure Snapshot (child from FigureLoop 1)
%Evaluate Matlab Expression (child from Report'mynamereport') ends the process
Is there a way to have these plots in the report in the order that they are generated? Many thanks!

Accepted Answer

Naty S
Naty S on 16 Jul 2015
Hey This is a bit of a workaround. Ran a for loop from i=1:1:length(findobj('Type','figure')); Inside the for loop i used the figure(i) function to open the figure and than I snapped shot it.
  1 Comment
Julio Martinez
Julio Martinez on 29 Jul 2015
Thank you Naty! I am back at it and I will try your solution. I had moved onto something else but this is still on my list of tasks to complete. I was told to use a MATLAB version prior to 2014b and works okay. However, I am interested in this workaround so that I can use R2015a which uses some functions that I believe are different in previous versions. I'll let you know how I did!

Sign in to comment.

More Answers (2)

Cindy Solomon
Cindy Solomon on 21 Apr 2015
Hi Julio,
Similar to how "drawnow" works when plotting figures, there is an equivalent function for the MATLAB Report Generator called "snapnow". Adding this line after the plot call in your loop should resolve the issue. For more information on this function, see this doc link.
Hope this helps!
  1 Comment
Julio Martinez
Julio Martinez on 22 Apr 2015
Edited: Julio Martinez on 22 Apr 2015
Thanks Cindy!
I tried the 'snapnow' function after checking your post and the background on it. Maybe I am misplacing it because it has not solve this issue. Here's what this part of my script looks like -The whole thing is long- so I am posting just these sections:
The 'for' starts the loop for processing a set of text files where data from MRI images is stored.
for nFile = 1:num
Fullname = [Pathname Filename{nFile}];
fID = fopen(Fullname,'r');
firstline = fgetl(fID); % has the sample name in it
n=10; % skip the rest of the header
while n
tline = fgetl(fID);
if ~ischar(tline), break, end
% disp(tline)
n=n-1;
end
% now read in the numbers
n = 1;
%AA = 0
while 1
[AA, count] = fscanf(fID, '%d %f %f %f %f %f %f %f %f %f %f %f %f',13);%there are 13 columns in each imported text file, file name plus 12 with numbers
if (count == 0), break, end
Series(n) = AA(1);
M(1,n) = AA(2);
A(1,n) = AA(3);
S(1,n) = AA(4);
M(2,n) = AA(5);
A(2,n) = AA(6);
S(2,n) = AA(7);
M(3,n) = AA(8);
A(3,n) = AA(9);
S(3,n) = AA(10);
M(4,n) = AA(11);
A(4,n) = AA(12);
S(4,n) = AA(13);
n = n + 1;
end
fclose(fID);
for n = 1:4
M1T1 = M(n,1:5)';%Pick values 1-5 in column
M1T2 = M(n,5:10)';%Pick values 5-10 in column, 5th value is recycled
%Uncertainties for T2 C(n) = 0; Csigma(n) = 0; T2(n) = 0; T2sigma(n) = 0; M0(n) = 0; M0sigma(n) = 0;
%Uncertainties for T1 SS(n) =0; SSsigma(n) = 0; T1(n) = 0; T1sigma(n) = 0; M0T1(n) = 0; M0T1sigma(n) = 0;
if any(M1T2(n) == 0), end; %continue, % no data so move onto the next column
[xData, yData] = prepareCurveData( T2taus, M1T2 );
ffunT2 = fittype('M0*exp(-x/T2) + C', 'independent', 'x', 'dependent', 'y');
options = fitoptions(ffunT2);
options.Algorithm = 'Levenberg-Marquardt';
options.Display = 'Off';
options.Robust ='Bisquare';
options.StartPoint = [1000 1 1000];
%Fit model to data.
[cfun, gof] = fit(xData,yData,ffunT2,options);
%Create a figure for the plots for T1
figure('Name', 'T2 fit 1,');
% Plot fit with data for T2.
% subplot( 2, 1, 1 ); not needed because we want larger figures
h = plot(cfun, xData, yData, 'ob' );
legend( h, 'Mean Signal Intensity vs. TE', 'Custom fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel 'Echo Time, TE (ms)'
ylabel 'Mean Signal Intensity, (ms)'
title 'T2'
grid on
snapnow
% Plot residuals.
figure('Name', 'T2 Residuals')
% subplot( 2, 1, 2 ); not needed because we want larger figures
h2 = plot( cfun, xData, yData, '*k','residuals');
legend( h2, 'Custom fit 1 - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel 'Echo Time,TE (ms)'
ylabel 'Mean Signal Intensity (ms)'
title 'T2'
grid on
snapnow
if any(M1T1(n) == 0), end; %continue, % no data so move onto the next column
%Curve fit for T1 (TR)
[xData2, yData2] = prepareCurveData( T1taus, M1T1 );
ffunT1 = fittype('M0T1*(1-SS*exp(-x/T1))', 'independent', 'x', 'dependent', 'y');
options = fitoptions(ffunT1);
options.Algorithm = 'Levenberg-Marquardt';
options.Display = 'Off';
options.Robust ='Bisquare';
options.StartPoint = [1000 1 1000];
%Fit model to data.
[cfun2, gof] = fit(xData2,yData2,ffunT1,options);
% Plot fit with data for T1.
figure('Name', 'T1 fit 1');
% subplot( 2, 1, 1 ); not needed because we want larger figures
h3 = plot(cfun2, xData2, yData2, 'sb' );
legend( h3, 'Mean Signal Intensity vs. TR', 'Custom fit 1', 'Location', 'SouthEast' );
% Label axes
xlabel 'Repetition Time, TR (ms)'
ylabel 'Mean Signal Intensity'
title 'T1'
grid on
snapnow
% Plot residuals for T1. figure('Name', 'T1 Residuals') % subplot( 2, 1, 2 ); not needed because we want larger figures
h4 = plot( cfun2, xData2, yData2, '*k','residuals');
legend( h4, 'Custom fit 1 - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel ('Repetition Time, TR (ms)')
ylabel ('Mean Signal Intensity')
title 'T1'
grid on
snapnow
C(n) = cfun.C;
M0(n) = cfun.M0;
T2(n) = cfun.T2;
uncert = confint(cfun, 0.683);
T2sigma(n) = (uncert(2,3) - uncert(1,3))/2;
Csigma(n) = (uncert(2,1) - uncert(1,1))/2;
M0sigma(n) = (uncert(2,2) - uncert(1,2))/2;
rsquared(n) = gof.rsquare;
chisquared(n) = (gof.rmse)^2;
T2Summary{n} = sprintf( '%.2f %.2f %.2f %f %.2f %.2f %f %.2f ', T2(n), T2sigma(n), M0(n), M0sigma(n), C(n), Csigma(n), rsquared(n), chisquared(n));
SS(n) = cfun2.SS;
M0T1(n) = cfun2.M0T1;
T1(n) = cfun2.T1;
uncert = confint(cfun2, 0.683);
T1sigma(n) = (uncert(2,3) - uncert(1,3))/2;
SSsigma(n) = (uncert(2,2) - uncert(1,2))/2;
M0T1sigma(n) = (uncert(2,1) - uncert(1,1))/2;
rsquared(n) = gof.rsquare;
chisquared(n) = (gof.rmse)^2;
T1Summary{n} = sprintf( '%.2f %.2f %.2f %.2f %.4f %.4f %f %.2f ', T1(n), T1sigma(n), M0T1(n), M0T1sigma(n), SS(n), SSsigma(n), rsquared(n), chisquared(n));
end
Series = sprintf( ' %d_%d ', Series(1), Series(10));%display(Series)
Patient = sprintf('%s ', pt);
Study = sprintf('%.f ', Stdy);
Temp = sprintf('%.f ', Te);
Range = sprintf('%.f ', Rnge);
Subject1 = sprintf('%s ', sub1);
State1 = sprintf('%s ', state1);
Tissue1 = sprintf('%s ', tiss1);
Subject2 = sprintf('%s ', sub2);
State2 = sprintf('%s ', state2);
Tissue2 = sprintf('%s ', tiss2);
Subject3 = sprintf('%s ', sub3);
State3 = sprintf('%s ', state3);
Tissue3 = sprintf('%s ', tiss3);
Subject4 = sprintf('%s ', sub4);
State4 = sprintf('%s ', state4);
Tissue4 = sprintf('%s ', tiss4);
fullLine = [Filename{nFile} Series Study Temp Range Temp Range Patient Subject1 State1 Tissue1 T1Summary{1} Subject2 State2 Tissue2 T1Summary{2} Subject3 State3 Tissue3 T1Summary{3} Subject4 State4 Tissue4 T1Summary{4} Subject1 State1 Tissue1 T2Summary{1} Subject2 State2 Tissue2 T2Summary{2} Subject3 State3 Tissue3 T2Summary{3} Subject4 State4 Tissue4 T2Summary{4} ];
fprintf(fsaveID,'%s \r\n', fullLine); %send report to .txt file in the order that the loop completes analysis
end % for nFile (this is the end for the loop)
I have tried placing the snapnow fx after h(n) = plot(xData...) In addition, I placed it after the last end having the same outcome. Any other ideas are most than welcomed!
Thanks a lot!

Sign in to comment.


Rob Edgar
Rob Edgar on 19 Dec 2018
Someone at Mathworks figured this out for me. I was not having success with contourf plots in a loop working in report generator. The trick for me was getImpl. Without that, every iteration of fig2 was a duplicated of the first one I had added.
fig2 = Figure(f); fig2.SnapshotFormat = 'png';
fig2 = getImpl(fig2, rpt);

Categories

Find more on Signal Generation and Preprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!