Add this line to your disp method and it may offer some insight.
When you display your 5 element object array, you'll see something that looks like this. I made the display format compact to avoid showing so many blank lines.
>> c
c =
ans =
1
ans =
[]
ans =
[]
ans =
[]
ans =
[]
myclass_object<a=1 b=>
myclass_object<a= b=>
myclass_object<a= b=2>
myclass_object<a= b=>
myclass_object<a= b=>
See the five instances of "ans = " in that display? What that line of code c.a creates is a comma-separated list. So in essence your fprintf statement acts like: fprintf('myclass_object<a=%.4g b=%.4g>\n', ...
c(1).a, c(2).a, c(3).a, c(4).a, c(5).a, ...
c(1).b, c(2).b, c(3).b, c(4).b, c(5).b);
That's why the second value (the value of the b property of the first element of the c array) displays as the b property of the third element of c. It's the sixth property value to be displayed.
In this case, a simple solution is to have your disp method iterate through the array of objects and display each one in turn.
>> dbtype myclass.m
1 classdef myclass
2 properties
3 a
4 b
5 end
6 methods
7 function disp(c)
8 for whichObj = 1:numel(c)
9 obj = c(whichObj);
10 fprintf('myclass_object<a=%.4g b=%.4g>\n', obj.a, obj.b)
11 end
12 end
13 end
14 end
>> c=myclass; c.a=1; c.b=2; c
c =
myclass_object<a=1 b=2>
>> c(5)=myclass
c =
myclass_object<a=1 b=2>
myclass_object<a= b=>
myclass_object<a= b=>
myclass_object<a= b=>
myclass_object<a= b=>
>> c(4).a=40
c =
myclass_object<a=1 b=2>
myclass_object<a= b=>
myclass_object<a= b=>
myclass_object<a=40 b=>
myclass_object<a= b=>
Best Answer