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

4 次查看(过去 30 天)
%%% 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.

采纳的回答

Matt J
Matt J 2013-6-6
编辑:Matt J 2013-6-6
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 个)

Andrew Newell
Andrew Newell 2013-6-6
编辑:Andrew Newell 2013-6-6
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);

类别

Help CenterFile Exchange 中查找有关 Sample Class Implementations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by