The CLASS function must be called from a class constructor.

8 ビュー (過去 30 日間)
Kristian Jørgensen
Kristian Jørgensen 2013 年 2 月 5 日
コメント済み: hossein 2017 年 6 月 24 日
I'm currently working on a project involving some automatic differentiation, and was presented an example code to such an approach. My problems really boil down to me never working with OOD in Matlab before and it seems really overwhelming. The first part of the code looks like this, fairly straight forward. The problem is it won't run past the last line.
function ad = autodiff(val, der)
% A naive autodiff constructor.
ad.val = val;
if nargin == 1
der = 0.0;
end
if strcmp(der,'variable')
der = 1.0;
end
ad.der = der;
ad = class(ad, 'autodiff');
I get the error 'The CLASS function must be called from a class constructor', which I do not know how to deal with. How do I create custom classes and then call upon them later?
thanks in advance, Kristian
  2 件のコメント
James Tursa
James Tursa 2013 年 2 月 6 日
Did you have this file in a directory named '@autodiff', and is the directory that the @autodiff folder is in on the MATLAB path?
hossein
hossein 2017 年 6 月 24 日
would you please explain more about the directory and @autodiff folder

サインインしてコメントする。

採用された回答

Cedric
Cedric 2013 年 2 月 5 日
編集済み: Cedric 2013 年 2 月 5 日
If you want to learn OOP in MATLAB, have a look at this document: MATLAB Object-Oriented Programming.
Basically, you want to create autodiff.m with a structure that will look like
classdef autodiff
properties
val
der
end
methods
function self = autodiff(val, der) % Constructor.
self.val = val;
if nargin == 1, self.der = 0.0; end
if strcmp(der,'variable'), self.der = 1.0; end
end
function self = aMethod(self, ...)
...
end
end
end
  2 件のコメント
Kristian Jørgensen
Kristian Jørgensen 2013 年 2 月 5 日
Thank you so much. Really helped me out, and I'll be sure to check out your link. Appreciate it.
Cedric
Cedric 2013 年 2 月 6 日
You're most welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by