How to import class definitions for typing

28 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2023-2-9
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.
Isaac Wolf
Isaac Wolf 2023-2-10
Hey Steven,
Thanks for the response. Here is the error and my setup:
Error defining property 'class1' of class 'SampleClass'. Class named 'OtherClass1' is undefined or does not support property validation.
Classes that do not support property validation include Java, COM classes, and MATLAB classes that do not use the classdef keyword.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2023-2-11
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.
  1 个评论
Isaac Wolf
Isaac Wolf 2023-2-14
Steven,
Thanks so much. I should have read more carefully. Funny enough I tried relative pathing like that, but I was fixated on importing first. So that error was till there. It's great that you can just reference like that relatively without importing.
Regarding the classdefs that's also good to know. At the moment I think we do want multiple files per class (because some of these functions are just giant); however, as we refactor and abstract out logic I think that could be a cleaner solution.
Thanks so much!
Isaac

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by