MATLAB: Error using fprintf Function is not defined for ‘struct’ inputs

fprintfstructures

Hi, I'm working on a problem for a homework assignment to print a cell array with a substructure. My code (I know its a bit lengthy, is as follows):
From3=struct('x',1,'y',2);
To3=struct('x',7,'y',5);
From2=struct('x',3,'y',5);
To2=struct('x',4,'y',7);
From1=struct('x',5,'y',6);
To1=struct('x',2,'y',10);
lineseg(3)=struct('Line',3,'From',From3,'To',To3);
lineseg(1)=struct('Line',1,'From',From2,'To',To2);
lineseg(2)=struct('Line',2,'From',From1,'To',To1);
fprintf('%-20s %-20s %-20s\n','Line','From','To')
for k=1:length(lineseg)
Line=lineseg(k).Line;
From=lineseg(k).From;
To=lineseg(k).To;
fprintf('%.1f %.1f %.1f\n', Line,From,To);
k=k+1
end
However, when I try to publish this, I get the error message: Error using fprintf Function is not defined for 'struct' inputs. I am 99% sure that it is due to the fact that there is a substructure, so how would i be able to publish this successfully?

Best Answer

You need to specify the field, x or y:
fprintf('%.1f %.1f %.1f\n', Line,From.x,To.x);
or
fprintf('%.1f %.1f %.1f\n', Line,From.y,To.y);
By the way, the k=k+1 at the end of the loop is not needed, and is, in fact, totally ignored by the code.
Also, by the way, there are no cell arrays in your code, only structures. So if you have to use cell arrays, this code will not meet the requirements. See the FAQ for a good description of cell arrays: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F