Importing multiple classes in single .m file in Matlab?

Viewed 2506

I've seen MATLAB: alternative of Octave's function 'source' (import m-file), but I cannot seem to figure out how the following works. This is for Matlab R2016b.

I'm looking at M_files_chap12.zip, and in that .zip, there are two files:

  • WDFDiodeExample.m which is runnable, and uses some additional classes
  • WDFClasses.m which contain the additional classes

Here is a snippet of 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
...

The problem is, I cannot seem to import these classes from this file. I have tried adding each of the following statements at start of WDFDiodeExample.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.

... and I get the errors written in the comments.

It seems that MATLAB wants only one-class-per-m-file, and it doesn't seem to recognize multiple classes packed in a single file - but then, why would the authors choose to write the WDFClasses.m like that so it is unusable directly? Or was this possible in earlier versions of MATLAB, and then got disabled?

1 Answers
Related