0

I'm trying to use object programming in Matlab. I only have one class, and one of its methods is expected to apply its only argument (an array) to modify internal values in the object.

However, I keep getting either "too many input arguments" or lack of some parameters when I call the method.

I have tried lots of tricks I found on the web, but to no avail. I must be missing some fundamental understanding of object programming in Matlab.

Here are the relevant parts of my code.

classdef K3MeshObject
    properties

        Xs 
        Ys 
        Zs 
        Ts 
        n
        m 

    end
    methods        
        % constructor:
        function obj=K3MeshObject(Xg,Yg,Zg)  % g stands for "given".
            if nargin == 3
                obj.Xs=Xg;
                obj.Ys=Yg;
                obj.Zs=Zg;
                obj.n=min([size(obj.Xs,1),size(obj.Ys,1),size(obj.Zs,1)]);
                obj.m=min([size(obj.Xs,2),size(obj.Ys,2),size(obj.Zs,2)]);
                obj.Ts=ones(obj.n,obj.m);
            end
        end

            % now the modifying method I cannot get to work:
            function K3TransMeshO(obj,K)       % << =====  tried with and without the obj argument
                for jj=1:obj.n                 % << =====  signals obj unknown. When obj removed, n unknown.
                    for ii=1:obj.m
                        V=K3Euclid(K*[obj.Xs(jj,ii);obj.Ys(jj,ii);obj.Zs(jj,ii);obj.Ts(jj,ii)]);
                        obj.Xs(jj,ii)=V(1);
                        obj.Ys(jj,ii)=V(2);
                        obj.Zs(jj,ii)=V(3);
                        obj.Ts(jj,ii)=V(4);
                    end
                end
            end
        end
    end
    function K3ShowTotem(wind, Xo)
    K3Header
    
    % Build the totem 
    Pillar = K3MeshObjectCopy(K3UnitCube)
    Pillar.K3TransMeshO(diag([2, 3, 1, 1]));   % <<======= problem call
    % I also tried K3TransMeshO(Pillar, diag([2, 3, 1, 1]))
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Xirdal
  • 67
  • 1
  • 10
  • 1
    If you read the linked answer, you’ll see you need to return `obj` from `K3TransMeshO()`, and call it as `pillar = pillar.K3TransMeshO(…)`, or more idiomatically as `pillar = K3TransMeshO(pillar, …)`, which is exactly the same thing. – Cris Luengo Jun 10 '23 at 13:36
  • Thank you, but it still won't work. I rewrote the code as: % Function definition: function obj=K3TransMeshO(obj,K) | obj.Xs=... ||| % Function call: Pillar=K3TransMeshO(Pillar,diag([2, 3, 1, 1])); ||| And this is the error message I get: ??? Error using ==> K3TransMeshO // Too many output arguments. // Error in ==> K3ShowTotem at 16 // Pillar=K3TransMeshO(Pillar,diag([2, 3, 1, 1])); – Xirdal Jun 11 '23 at 06:19
  • 1
    You might have to reload the class definition. Normally this happens automatically, not sure why it didn't do that for you. Do `clear K3MeshObject` or `clear classes`. – Cris Luengo Jun 11 '23 at 07:43
  • My program begins with "clear". Doesn't it clear classes as well? – Xirdal Jun 11 '23 at 14:22
  • "clear classes" worked like a charm, thanks! – Xirdal Jun 11 '23 at 14:32
  • `clear` only clears variables from the current workspace. `clear all` also clears function definitions, but not classes. It’s a weird historical artifact I guess. – Cris Luengo Jun 11 '23 at 15:12

0 Answers0