Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to fix "Undefined variable "P2" or class "P2.Tilkny​tMedarbejd​er""?

Asked by Jes on 25 Apr 2013

I am working on a university projekt, that is giving me some issues.

I am trying to creat a Matrix that i can change the values in. I have managede to get a Matrix started that looks like this:

ans =

     0     1     2     3     4     5     6
     1     0     0     0     0     0     0
     2     0     0     0     0     0     0
     3     0     0     0     0     0     0
     4     0     0     0     0     0     0

It is created through a class constructor (i will copy the whole script file in here later)

I then have to make a function that will change the 0 in (2,2) to a 2

so i have made this script file with the function tilknytmedarbejder (i am a dane so some of the variable and function names are in danish)

classdef Projekt2 <handle properties Projekter

    end
    methods (Access=public)
        function P2=Projekt2 (antalprojekter,antalmedarbejdere)
            P2.Projekter=zeros(antalprojekter+1,antalmedarbejdere+1);
            P2.Projekter(1,:)=0:6;
            P2.Projekter(:,1)=0:4;
        end
        function TilknytMedarbejder (P2,Projekt,Medarbejder)
            P2.Projekter(Projekt+1,Medarbejder+1)=2;
    end
    end
end

When i try to call the function TilknytMedarbejder by writing in the command windue P2.TilknytMedarbejder(1,1) (because i have it adding one to both of the variables) i get the error message: "Undefined variable "P2" or class "P2.TilknytMedarbejder""?

is there a simple solution to this???

please help me since i am no expert but eager to learn

best regards

0 Comments

Jes

Products

No products are associated with this question.

1 Answer

Answer by per isakson on 25 Apr 2013
Edited by per isakson on 25 Apr 2013
Accepted answer

Your code works! Did you miss to create the object, myP2?

    >> myP2 = Projekt2( 4, 6 );
    >> myP2.TilknytMedarbejder( 1, 1 )
    >> myP2.Projekter
    ans =
         0     1     2     3     4     5     6
         1     2     0     0     0     0     0
         2     0     0     0     0     0     0
         3     0     0     0     0     0     0
         4     0     0     0     0     0     0

where

    classdef Projekt2 < handle 
        properties 
            Projekter
        end
        methods (Access=public)
            function P2=Projekt2 ( antalprojekter, antalmedarbejdere )
                P2.Projekter = zeros( antalprojekter+1, antalmedarbejdere+1 );
                P2.Projekter(1,:)=0:6;
                P2.Projekter(:,1)=0:4;
            end
            function TilknytMedarbejder (P2,Projekt,Medarbejder)
                P2.Projekter(Projekt+1,Medarbejder+1)=2;
            end
        end
    end

1 Comment

Jes on 25 Apr 2013

Dont know why but it worked :D :D :D :D

You saved my week, and my butt at university :D

Thank you sooooooooo much :D

per isakson

Contact us