Innerjoin when a table contains user-defined objects
Show older comments
I have two tables that I wish to innerjoin() according to the topThick and botThick columns, which are the same in both tables. Why does this fail when Tright contains a column with objects of a user-defined class (here, myclass)?
load testdata
Tleft, Tright
innerjoin(Tleft, Tright)
Accepted Answer
More Answers (1)
7 Comments
Paul
on 13 Jun 2026 at 19:45
Why not modify the class definition to accept zero arguments in the constructor, if you don't mind me asking?
Paul
28 minutes ago
One possbility to avoid the need to create two temporary tables would be to use dbstack in the zero-argument call to the myclass constructor and take appropriate action if innerjoin is in the stack and error otherwise.
Paul
25 minutes ago
Yes, return an empty object, whatever that might mean for the object at hand. Maybe an object with all properties empty of the appropriate type.
The objects created with the no-arg constructor are used; they ulitmately become a variable in the table returned from innerjoin.
It's like a pre-allocation thing. Create an array of empty objects
out = repmat(myclass(),N,1) % not the exact code, just showing the idea
Then fill in the output with selected values of the input
out(indexout) = in(indexin) % in is a vector of myclass pulled from the input table
indexout is logical and indexin is numerical. out is then returned as variable in the table returned from innerjoin.
For an inner join, I think that indexout is always
indexout = true(N,1)
in which case we could simply have
out = in(indexin)
which would be more efficent as I said previously.
But, for an outer join indexout could (most likely would) have some false entries and the outer joined table would typically include "default" values in the variables, e.g., NaN for double and, I presume, myClass() for myClass.
As I said previously, at the point in the code of the indexing there is no distinction between outer and inner joins, which isn't to say that there couldn't be.
Also, key variables can be returned with default values for outer joins. Not sure if outer joins or myClass key variables are of interest.
Paul
8 minutes ago
"... I find it questionable ..."
I agree.
Would be curious to know what response you get from Tech Support.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!