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

4 views (last 30 days)
%%% 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.

Accepted Answer

Matt J
Matt J on 6 Jun 2013
Edited: Matt J on 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().

More Answers (1)

Andrew Newell
Andrew Newell on 6 Jun 2013
Edited: Andrew Newell on 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);

Categories

Find more on Sample Class Implementations 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!