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일
Good point about using the constructor. For some reason the first two answers weren't visible when I added mine.

댓글을 달려면 로그인하십시오.

추가 답변 (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