I want to initialize class variables through array type class object using a function

4 Ansichten (letzte 30 Tage)
%%% Code for my class Nodes.m%%%%%%%%%%
classdef Nodes
% Detailed explanation goes here
% The node class contains the properties of each node
properties (SetAccess = public)
NodeID
x
y
p=dlnode
noNodes = 100
uid = (1:100)
InitialEnergy
ResidualEnergy
mark=0
NodeStatus;
Distance
Dst
end
end
%%%%%%%creating array type class object%%%%%
n(1, noOfNodes)= Nodes;
%%%%%%Function to initialize class variables%%%%
function Initialize( n,i,noOf Nodes)
for i=1:noOfNodes
n(i).NodeID=i;
x=rand(); %j;
y=rand(); %k;
n(i).x= x;
n(i).y= y;
n(i).InitialEnergy=2400.0;
n(i).NodeStatus=0;
n(i).mark=0;
n(i).ResidualEnergy=n(i).InitialEnergy;
n(i).NodeID= num2str(i);
end
end
%%%%%%%script file Test.m to call initialize funtion%%%%%%
clc;
clear;
% Initialization of Global Variables
Initialize( n,i,noOfNodes);
n = Nodes;
noOfNodes = 35; %No. of Nodes
n.noNodes = noOfNodes;
NG = 3; %No. of Geocst Regions
GX = 500; % Maximum Value of x-coordinate
GY = 500; %Maxium Value of y-coordinate
LX = 0; % Minimum Value of x-coordinate
LY = 0; % Minimum Value of y-coordinate
The problem here is that though the variables get initializes using the function but they are not being stored in the work space. If I display them in the function I get the value but if work space the variables are not getting any values.

Akzeptierte Antwort

Matt J
Matt J am 6 Jun. 2013
Bearbeitet: Matt J am 6 Jun. 2013
becuase Initialize is not returning any output argument to the workspace. You need this instead
n = Initialize( n,i,noOfNodes);
and
function n = Initialize( n,i,noOf Nodes)
.....
end
Incidentally, it is curious that you do not define a class constructor, choosing instead to use the external function Initialize().
  1 Kommentar
Andrew Newell
Andrew Newell am 6 Jun. 2013
Good point about using the constructor. For some reason the first two answers weren't visible when I added mine.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrew Newell
Andrew Newell am 6 Jun. 2013
Bearbeitet: Andrew Newell am 6 Jun. 2013
The main problem is that in the command
Initialize( n,i,noOfNodes);
the output isn't assigned to n. You need
n = Initialize( n,noOfNodes);
Note that I have removed i from the list of arguments, because i is an internal variable. Also, you assign values to NodeID twice inside the loop:
n(i).NodeID=i;
...
n(i).NodeID= num2str(i);
Which one do you want?
It's a good idea to make Initialize a method, as follows:
classdef Nodes
% Detailed explanation goes here
% The node class contains the properties of each node
properties (SetAccess = public)
NodeID
x
y
p=dlnode
noNodes = 100
uid = (1:100)
InitialEnergy
ResidualEnergy
mark=0
NodeStatus;
Distance
Dst
end
methods
function n = Nodes(noOfNodes)
if nargin > 0
for i=noOfNodes:-1:1
n(i).NodeID=i;
n(i).x= rand;
n(i).y= rand;
n(i).InitialEnergy=2400.0;
n(i).NodeStatus=0;
n(i).mark=0;
n(i).ResidualEnergy=n(i).InitialEnergy;
end
end
end
end
end
Then you could run it using commands like this:
noOfNodes = 35;
n = Nodes(noOfNodes);

Kategorien

Mehr zu Sample Class Implementations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by