在Matlab中导入单个.m文件中的多个类?

时间:2017-05-23 21:24:53

标签: matlab

我已经看过MATLAB: alternative of Octave's function 'source' (import m-file),但我似乎无法弄清楚以下是如何运作的。这是Matlab R2016b。

我正在查看M_files_chap12.zip,而在.zip中,有两个文件:

  • Lint/AmbiguousBlockAssociation: Exclude: - 'spec/**/*' Include: - 'spec/support/**/*' - 'spec/factories/**/*' - 'spec/rails_helper.rb' - 'spec/spec_helper.rb' 可运行,并使用其他一些类
  • WDFDiodeExample.m,其中包含其他类

以下是WDFClasses.m

的摘录
WDFClasses.m

问题是,我似乎无法从此文件导入这些类。我尝试在% WDFclasses.m % .... %----------------------WDF Class------------------------ classdef WDF < hgsetget % the WDF element superclass properties PortRes % the WDF port resistance end methods function Volts = Voltage(obj) % the voltage (V) over a WDF element Volts = (obj.WU+obj.WD)/2; % as defined in the WDF literature end end; end %----------------------Adaptor Class------------------------ classdef Adaptor < WDF % the superclass for ser. and par. (3-port) adaptors properties KidLeft % a handle to the WDF element connected at the left port KidRight % a handle to the WDF element connected at the right port end; end ... 开头添加以下每个语句:

WDFDiodeExample.m

...我收到了评论中写的错误。

似乎MATLAB想要一个每类m文件,并且它似乎无法识别打包在单个文件中的多个类 - 但是,为什么会作者选择这样写WDFClasses %this will run WDFClasses.m; but % Class name and filename do not agree. eval(fileread('WDFClasses.m')) % Error: Illegal use of reserved keyword "classdef". run('WDFClasses.m') % Class name and filename do not agree. 所以它直接无法使用?或者这在早期版本的MATLAB中是否可行,然后被禁用?

1 个答案:

答案 0 :(得分:1)

事实证明,相关文献也提到了这一点:

  

必须注意的是,所有呈现的类都显示在a中   单个M文件的紧凑性虽然在实践中MATLAB要求每个类都驻留在一个   个人档案。换句话说,类......应该分成七个不同的文件   为了让模型在MATLAB中运行。

...所以我想,这就是它的工作方式......

相关问题