how to fix "Undefined variable "P2" or class "P2.TilknytMedarbejder""?
4 views (last 30 days)
Show older comments
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
Accepted Answer
per isakson
on 25 Apr 2013
Edited: per isakson
on 25 Apr 2013
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
More Answers (0)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!