Create objects in a loop
5 次查看(过去 30 天)
显示 更早的评论
Hello, i have a class with properties and methods, i want to calculate somtehing, which is a method in my class, by varying a property in my class. Is it possible to create a loop that creates as objects as the length of my property, and then use the method for every object created ?
I could do it manually but for i=10:1:5000, it takes a long long time...
Here are the parts of my code for this question the property i want to vary is "N", the function that calculates what i want is "integ_sigma"
classdef pot_flow_class
properties
U;
H;
N;
Ng;
r;
th;
sigma;
% thp;
X;
Y;
nb_iso;
interp;
end
methods
function obj = pot_flow_class(valH,valN,valNg,valR,valU,val_nb_iso)
obj.U = valU;
obj.H = valH;
obj.N = valN;
obj.Ng = valNg;
obj.r = valR;
obj.nb_iso = val_nb_iso;
obj.th=0:pi/(valN-1):pi;
% sigma pour le pb infini
obj.sigma=sin(obj.th)/pi;
obj.X=linspace(-10,10,valNg);
obj.Y=linspace(-200,200,valNg);
end
function obj = calcul_source(obj)
RC = obj.r*cos(obj.th);
RS = obj.r*sin(obj.th);
RC = RC(2:obj.N-1);
RS = RS(2:obj.N-1);
Mat_A = pi*eye(obj.N-2);
for i=1:obj.N-2
for j=1:obj.N-2
Mat_A(i,j)= Mat_A(i,j)+...
pi/obj.N*...
(RC(i)*(RC(i)+RC(j)-2*obj.H)+RS(i)*(RS(i)-RS(j)))/((RC(i)+RC(j)-2*obj.H)^2+(RS(i)-RS(j))^2)...
-pi/obj.N*...
(RC(i)*(RC(i)+RC(j)-2*obj.H)+RS(i)*(RS(i)+RS(j)))/((RC(i)+RC(j)-2*obj.H)^2+(RS(i)+RS(j))^2);
end
end
obj.sigma(2:obj.N-1)=linsolve(Mat_A,-RS');
end
function integ_sigma(obj)
S = trapz(obj.th,obj.sigma.*obj.sigma);
disp(S)
end
end
end
2 个评论
Matt J
2018-8-4
to create a loop that creates as objects as the length of my property, and then use the method for every object created ?
There is no object creation shown in integ_sigma, so it is not clear what you mean. If you want to create multiple objects, you should do that in the constructor.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!