필터 지우기
필터 지우기

How to import class definitions for typing

조회 수: 28 (최근 30일)
Isaac Wolf
Isaac Wolf 2023년 2월 9일
댓글: Isaac Wolf 2023년 2월 14일
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