Reputation: 4199
I am just approaching Matlab, I have a function with a struct:
function [out] = struct1()
Account(1).name = 'John';
Account(1).number = 321;
Account(1).type = 'Current';
%.......2 to 9
Account(10).name = 'Denis';
Account(10).number = 123;
Account(10).type = 'Something';
for ii= 1:10
out=fprintf('%s\n','%d\n','%s\n',Account{ii}.name, Account{ii}.number,Account{ii}.type);
end
end
The above code gives me an error: "Cell contents reference from a non-cell array object."
How do I output all elements of such struct to get this output using "fprintf"?
name: 'John'
number: 321
type: 'Current'
...... 2 to 9
name: 'Denis'
number: 123
type: 'Something'
Upvotes: 0
Views: 2835
Reputation: 8401
You are indexing the elements of the struct array with {
and }
which are only used for cell arrays. Simple (
and )
will work just fine.
Also, since you have the line breaks in the formatspec
, you should just combine all three strings together.
Example:
formatspec = 'name: %s\nnumber: %d\ntype: %s\n';
for ii= 1:10
out=fprintf(formatspec,Account(ii).name,Account(ii).number,Account(ii).type);
end
Upvotes: 3