How to import class definitions for typing
28 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I have a class that's composed of several other classes. How are you supposed to import additional definitions in the properties section of the class? For example
classdef SampleClass
properties
class1 (1,1) OtherClass1
class2 (1,1) OtherClass2
end
end
I tried the obvious things: before the classdef, even in the classdef and properties. None of which seem to work. I hope there's something I missing!
It's a bit frustrating since obviously in other languages this is trivial. I'm trying to be amendable to different ways of doing things, so if using namespaces is not the way to organize code please let me know.
Regards,
Isaac
2 Commenti
Steven Lord
il 9 Feb 2023
Assuming you have classes named OtherClass1 and OtherClass2 in files OtherClass1.m and OtherClass2.m that are not in package directories, I would expect what you've written to work. Though there are a few caveats particularly if those classes are handle classes; see this documentation page and this one.
Can you say a little more about what difficulties you're encountering in your code, particularly with a more complete example? If you see error or warning messages the full and exact text of those messages would be useful as well.
Risposta accettata
Steven Lord
il 11 Feb 2023
The full name of that class is not OtherClass1. It is subdir.OtherClass1. This insulates you from the possibility that some other package directory has its own class in a file OtherClass1.m that you may or may not be aware of.
I had the statement "that are not in package directories" in my previous comment, but your class is in a package directory.
FYI, in the older object system classes were required to be in directories whose names start with the @ symbol followed by the name of the class. In the classdef-based object system this is no longer a requirement (unless you want to define your class methods across multiple files, as stated on this documentation page.) So you could have your OtherClass1.m directly in the +subdir directory if you wanted. Similarly you could have SampleClass in the same directory as the +subdir directory. This flattens your directory structure a little bit.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Software Development Tools in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!