MATLAB: Hi I have a short code I wrote but the plot function does not show any data would someone take a look

MATLAB and Simulink Student Suiteploting

apsize= 0.182;
apsize= apsize+0.000000001;
Xspacing =.001;
z = 0;
k = -0.871; % conic constant
r = 0.066; % radius of curvature
a2= 0; a4= 120; a6=0 ; % coeff of higher order terms
c= 1/r;
fid = fopen('sphere.NC','w');
hold off
fprintf(fid, 'G01\r\nG71\r\nG90\r\nG94\r\nG14\r\n' ); % g code header







fprintf(fid, 'T0101\r\n' ); % g code header
fprintf(fid, 'M4S4000\r\n' ); % g code header
fprintf(fid, 'M26\r\n' ); % g code header
while apsize >= 0.000000
x2= apsize;
z = -1*(((c*(x2.^2))./(1+sqrt(1-((1+k)*c*c*x2.^2))))+(a2*x2.^2)+(a4*x2.^4)+(a6*x2.^6));
plot(x2,z,'red','LineWidth',2.4);
hold on;
(fprintf(fid, 'X%0.8f Z%0.8f \r\n', x2 , z ));
apsize = apsize-Xspacing;
end
fprintf(fid, 'M29\r\n' ); % g code header
fprintf(fid, 'M5\r\n' ); % g code header
fprintf(fid, 'M30\r\n' ); % g code header
fprintf(fid, ' \r\n' ); % g code header
fclose(fid);

Best Answer

Try this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
apsize= 0.182;
apsize= apsize+0.000000001;
Xspacing =.001;
z = 0;
k = -0.871; % conic constant
r = 0.066; % radius of curvature
a2= 0; a4= 120; a6=0 ; % coeff of higher order terms
c= 1/r;
fid = fopen('sphere.NC','w');
hold off
fprintf(fid, 'G01\r\nG71\r\nG90\r\nG94\r\nG14\r\n' ); % g code header







fprintf(fid, 'T0101\r\n' ); % g code header
fprintf(fid, 'M4S4000\r\n' ); % g code header
fprintf(fid, 'M26\r\n' ); % g code header
index = 1;
while apsize >= 0.000000
x2(index) = apsize;
z(index) = -1*(((c*(x2(index).^2))./(1+sqrt(1-((1+k)*c*c*x2(index).^2))))+(a2*x2(index).^2)+(a4*x2(index).^4)+(a6*x2(index).^6));
fprintf(fid, 'X%0.8f Z%0.8f \r\n', x2 , z );
apsize = apsize-Xspacing;
index = index + 1;
end
plot(x2,z,'r.-','LineWidth', 2, 'MarkerSize', 20);
grid on;
title('z vs. x2', 'fontSize', fontSize);
xlabel('x2', 'fontSize', fontSize);
ylabel('z', 'fontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
fprintf(fid, 'M29\r\n' ); % g code header
fprintf(fid, 'M5\r\n' ); % g code header
fprintf(fid, 'M30\r\n' ); % g code header
fprintf(fid, ' \r\n' ); % g code header
fclose(fid);
msgbox('Done with program');