MATLAB: Need help creating a chart using fprintf

fprintfMATLAB

I'm having trouble creating a chart using the fprintf command. I'm trying to create one by limiting myself to three fprintf commands. This is the code that I'm using:
x = [1:5];
y = [1:3];
[X,Y] = meshgrid(x,y);
z = X.^2 + Y.^2;
t = z';
i = t(:,1);
j = t(:,2);
k = t(:,3);
fprintf(' y = 1 2 3 \n');
fprintf(repmat('-',1,35),'\n');
fmt = ['x = %5.0f | \n', x'];
fmt1 = ['%5.0f %5.0f %5.0f \n', [i,j,k]]
fprintf(fmt, fmt1)
It is supposed to look something like this
I appreciate any help.
Thank you

Best Answer

fmt = 'x = %5.0f | %5.0f %5.0f %5.0f \n';
fprintf(fmt, [x(:),i,j,k].' )
Notes:
  • if you have multiple lines that are being presented once, they can all go in the same format
  • everything that is repeated once per array member, should be put together in a single format
  • elements taken from input by going down rows first -- the same order as linear indexing. So to print rows, prepare the data as rows and then use .' to turn it into columns