Clear Filters
Clear Filters

Figure changes during export

27 views (last 30 days)
Nathan Hogaboom
Nathan Hogaboom on 6 Aug 2024 at 17:54
Commented: Nathan Hogaboom on 7 Aug 2024 at 16:32
Hello everyone. I have created a loop where my function would save a plot of a series of posterior tangents overlaying vertebrae of the cervical spine. The problem is, after my function runs (code displayed below) only 5 out of the 6 lines appear on the plot. If I run it in isolation in the command window, all 6 lines appear. I have attached the images, the plot pre export with all 6 lines (ran in command window) and post export with the incorrect number (run as a function). I have gone through a number of times and cannot figure out why one line disappears when exporting. Any ideas?
Thanks in advance.
PS I have commented out parts of the code that are not relevant for this question. I did not delete so you were not confused about the input/output variables of my function.
function [c1_c7_angle,c2_c7_angle,c2c3_tangent,c3c4_tangent,c4c5_tangent,c5c6_tangent,c6c7_tangent]=rotation_angles(c1x,c1y,c2x,c2y,c3x,c3y,c4x,c4y,c5x,c5y,c6x,c6y,c7x,c7y,new_filename,i2)
%define posterior corners.
c2post=[c2x(1) c2y(1); c2x(4) c2y(4)];
c3post=[c3x(1) c3y(1); c3x(4) c3y(4)];
c4post=[c4x(1) c4y(1); c4x(4) c4y(4)];
c5post=[c5x(1) c5y(1); c5x(4) c5y(4)];
c6post=[c6x(1) c6y(1); c6x(4) c6y(4)];
c7post=[c7x(1) c7y(1); c7x(4) c7y(4)];
%c2_angle=rad2deg(atan((c2post(2,2)-c2post(1,2))/(c2post(2,1)-c2post(1,1))));
%c3_angle=rad2deg(atan((c3post(2,2)-c3post(1,2))/(c3post(2,1)-c3post(1,1))));
%c4_angle=rad2deg(atan((c4post(2,2)-c4post(1,2))/(c4post(2,1)-c4post(1,1))));
%c5_angle=rad2deg(atan((c5post(2,2)-c5post(1,2))/(c5post(2,1)-c5post(1,1))));
%c6_angle=rad2deg(atan((c6post(2,2)-c6post(1,2))/(c6post(2,1)-c6post(1,1))));
%c7_angle=rad2deg(atan((c7post(2,2)-c7post(1,2))/(c7post(2,1)-c7post(1,1))));
%c2c3_tangent=c2_angle-c3_angle;
%c3c4_tangent=c3_angle-c4_angle;
%c4c5_tangent=c4_angle-c5_angle;
%c5c6_tangent=c5_angle-c6_angle;
%c6c7_tangent=c6_angle-c7_angle;
%calculate c21-c7 angle by defining angle of inferior vertebral points.
%c1_angle=rad2deg(atan((c1y(1)-c1y(2))/(c1x(2)-c1x(1))));
%c2_inf_ang=rad2deg(atan((c2y(3)-c2y(4))/(c2x(3)-c2x(4))));
%c7_inf_ang=rad2deg(atan((c7y(3)-c7y(4))/(c7x(3)-c7x(4))));
%c1_c7_angle=c7_inf_ang-c1_angle;
%c2_c7_angle=c7_inf_ang-c2_inf_ang;
vertebra_matrix=[c2post;c3post;c4post;c5post;c6post;c7post];
post_x=[c2x(1) c2x(4) c3x(1) c3x(4) c4x(1) c4x(4) c5x(1) c5x(4) c6x(1) c6x(4) c7x(1) c7x(4)];
post_y=[c2y(1) c2y(4) c3y(1) c3y(4) c4y(1) c4y(4) c5y(1) c5y(4) c6y(1) c6y(4) c7y(1) c7y(4)];
theta=zeros([1 6]);
x_diff=zeros([1 6]);
y_diff=zeros([1 6]);
V=zeros([6 2]);
factor_distance=2;
imshow(i2,'InitialMagnification',100);
hold on;
for h=1:6
f=h*2;
d=f-1;
x_diff(h)=post_x(d)-post_x(f);
y_diff(h)=post_y(f)-post_y(d);
theta(h)=rad2deg(atan(y_diff/x_diff));
V(h,:)=vertebra_matrix(d,:)-vertebra_matrix(f,:);
pext(h,:)=vertebra_matrix(h,:)+V(h,:)*factor_distance;
ax=gca;
output_filename=sprintf('%s_rotation.tif',new_filename);
exportgraphics(ax,output_filename);
plot([post_x(f),pext(h,1)],[post_y(f),pext(h,2)],'r-')
end
end

Accepted Answer

dpb
dpb on 6 Aug 2024 at 18:36
Moved: dpb on 6 Aug 2024 at 18:36
exportgraphics(ax,output_filename);
plot([post_x(f),pext(h,1)],[post_y(f),pext(h,2)],'r-')
The exportgraphics call is before the call to plot() so the last one hasn't yet been plotted...when the loop finishes at the command line, then the line shows on the image--but that last image isn't exported.
Reverse the order of those two lines and joy should/will ensue...
  4 Comments
dpb
dpb on 6 Aug 2024 at 21:11
Edited: dpb on 7 Aug 2024 at 16:28
OBTW, it would be more efficient to move the line
exportgraphics(ax,output_filename);
outside after the loop; no sense in rewriting the same file six times...one piece at a time; the final plot is the only one you're really interested in.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!