Matlab: Import functions into a class

I have a class file in Matlab.

I created the i directory structure using the package specifications.

+MyPkg |--+F1 |--+F2 |--+F3 | |--fun.m |--myc.m 

My class is myc and it is inserted in the MyPkg package.

The fun function is stored in the F3 in the main.

I want to use the fun function in my class. How???

+2
source share
2 answers

You need to reference fun as MyPkg.F3.fun everywhere. Unfortunately, full packages should be used explicitly everywhere in MATLAB (or you should use import statements).

+2
source

The way you describe using classes is the "old" way to do this in Matlab. I don’t know how it all works when you use the "old" method, but class files make life easier. I highly recommend them. Thus, you can put all the functions for the class into a single file. For example, you can create a file:

myclass.m

 classdef myclass methods function out=add(a,b) out=a+b end function out=subtract(a,b) out=ab end end end 

If you put myclass.m in the same folder as your m file. Then you can access the class as follows:

 a=5; b=3; asdf=myclass; c=asdf.add(a,b) d=asdf.subtract(a,b) 

A more extensive example is given at the following link:

http://www.mathworks.com/help/techdoc/matlab_oop/brhzttf.html

I hope this helps.

-1
source

Source: https://habr.com/ru/post/1485660/


All Articles