Error using fprintf Function is not defined for 'struct' inputs?
11 views (last 30 days)
Show older comments
Katarina Bethel
on 8 Apr 2014
Answered: Image Analyst
on 8 Apr 2014
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?
0 Comments
Accepted Answer
Image Analyst
on 8 Apr 2014
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Structures 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!