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

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

 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

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

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Asked:

Jes
on 25 Apr 2013

Community Treasure Hunt

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

Start Hunting!