Evaluating Method for multiple Objects, without using Eval
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ivan Lizat
am 5 Dez. 2020
Beantwortet: Ivan Lizat
am 6 Dez. 2020
Good Evening together !
I am working with two classes:
Class I
Properties : temperature
pressure
components ( is a cell array of objects of the Class 2)
%Varying size dependent of No of Components in the Class 1
Class 2
methods : saturationPressure(obj, Temp)
classdef Class 1
properties
pressure
temperature
molarFlow
components
end
.
.
.
function value = boilingPoint(obj,T)
.
.other_functionstufff .
.
. % Need to evaluate the saturationPressure method of the the given components
for i = 1:numel(obj.components)
obj.#{components}#.saturationPressure.(obj.components{i},T)
end
.
.
Three Questions:
- Is there a way to evaluate the saturationPressure method of Class2 objects for all components of Class 1, without evalin?
2. Is there generally a better approach to evaluate methods of Objects inside a method of another Class
3. Or is there a obvious more clever way to achieve the same task (other datatype of components or similar )?
I would be very thankfull for help, i have the feeling i am oversseing something obvious -.-'
Thank You!
0 Kommentare
Akzeptierte Antwort
per isakson
am 6 Dez. 2020
Bearbeitet: per isakson
am 6 Dez. 2020
Q1: Yes (I don't understand why you thought of using eval/evalin.)
Q2: ???
Q3: Yes, e.g. array of instancies of Class_2
The demo below addresses your questions. It's nonsence from the point of view of physical chemistry, but will hopefully help you use Matlab.
>> c1 = Class_1(4);
>> c1.boilingPoint(112)
ans =
54689 95751 96489 15762
>>
where
classdef Class_1 < handle
properties
pressure
temperature
molarFlow
components Class_2 % array of instancies of Class_2
end
methods
function this = Class_1( N )
this.components(1:N) = Class_2();
end
function value = boilingPoint( this, T )
% Evaluate the saturationPressure method of the given components
value = [];
for obj = this.components
% obj.#{components}#.saturationPressure.(obj.components{i},T)
value(end+1) = obj.saturationPressure( T ); %#ok<AGROW>
end
end
end
end
and
classdef Class_2
properties
end
methods
function val = saturationPressure( this, T )
val = randi( 1e5, 1 );
end
end
end
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Identification 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!