Class Unexpected Behaviour with Properties and Type Casting
6 views (last 30 days)
Show older comments
Hello Community,
Is this the expected behavior when having a property in a class that is type declared and then re-assigned to a different type?
classdef Blah
properties
meh char = '';
end
methods
function obj = Blah()
obj.meh = {'Bleah!', 'Bleah, again'};
end
end
end
Here is the screenshot of the result of the class instantiation:

Shouldn't this code produce at least a runtime error when meh is assigned to a cell object instead of forcing this automatic typecasting?
This gives an error (expected)
Is this my old version on matlab2018b that works in this confusing way?
It defeats the purpose of declaring the type because accidental/wrong assignments to different types go undetected.
0 Comments
Accepted Answer
Steven Lord
on 12 Sep 2025
Shouldn't this code produce at least a runtime error when meh is assigned to a cell object instead of forcing this automatic typecasting?
No. From the most recent documentation (but I believe the documentation for release R2018b should read basically the same):
"Defining the class of a property can reduce the need to test the values assigned to the property in your code. In general, the value assigned to the property must be of the specified class or convertible to the specified class. An exception to this rule is for subclasses. If you specify a user-defined class as part of validation, subclasses of that class pass validation without error, but they are not converted." [Emphasis added.]
type sampleclass2179908.m
y = sampleclass2179908('abcde')
y = sampleclass2179908({'abcde'})
3 Comments
Steven Lord
on 12 Sep 2025
If you declared that a property was supposed to contain, say, an int32 would you expect to be able to store a literal 1 in it? What class is the literal 1?
classOfLiteralOne = class(1)
We could have forced you to explicitly convert the literal 1 into an int32 in order to store it into the property of the array.
oneDouble = 1
oneInt32 = cast(oneDouble, 'int32') % or
oneInt32 = int32(oneDouble)
whos one*
If you use the mustBeA approach, you will in fact be forced to do the explicit conversion.
try
mustBeA(1, 'int32')
fprintf("mustBeA call succeeded.")
catch ME
fprintf("mustBeA call failed with error:\n%s\n", ME.message)
end
But in this case, it's clear what you intend to do: store the value one (I'm using this to refer to the value independent of the class used to store it) in the property. So MATLAB does the conversion for you. There are other places where we do this type of implicit conversion as well: should we force you to use oneInt32 instead of oneDouble in the code below that creates a variable B?
A = ones(2, 'int32')
B1 = A + oneDouble
B2 = A + oneInt32
whos A B*
Your case is slightly different but very similar. There are at least three different ways to store textual data in MATLAB: char arrays, cell arrays containing char vectors (also known as cellstrs), or string arrays. You can see this category in the documentation for an explanation of the differences between the three representations. Each has its pros and cons. But generally speaking, most MATLAB functions ought to be able to handle each of the three representations in a relatively reasonable way (where "relatively reasonable" could include converting between the representations.) See this documentation page for some guidance, and note that most of our functions follow the guidance on this page.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!