user128350
user128350

Reputation: 39

Matlab class basics

I'm having some trouble creating classes in matlab and I don't really understand the method behind it (i'm fairly new to it) here is my attempt at basic addition using matlab

classdef test

properties

   a 
   b    

end

methods

   function add = plus(a, b)
   end

end

end

assigning values via

 p=test(), p.a=5 

etc seems to work fine, however attempting p.add returns the error

No appropriate method, property, or field add for class test. 

Any help or guidance would be appretiated, thanks.

Upvotes: 1

Views: 165

Answers (1)

Jonas
Jonas

Reputation: 74940

Methods are defined exactly as functions are with respect to names and outputs.

Therefore, the method is called plus, the output the method should calculate is called add, and the way you probably wanted to write the method is:

function out = add(this)
     out = this.a + this.b;
end

Now you call the method as

p.add();

Upvotes: 2

Related Questions