Evaluating Method for multiple Objects, without using Eval

조회 수: 4 (최근 30일)
Ivan Lizat
Ivan Lizat 2020년 12월 5일
답변: Ivan Lizat 2020년 12월 6일
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:
  1. 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!

채택된 답변

per isakson
per isakson 2020년 12월 6일
편집: per isakson 2020년 12월 6일
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

추가 답변 (1개)

Ivan Lizat
Ivan Lizat 2020년 12월 6일
I got it, the solution was, as you proposed, to use object arrays.
Thank you !

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by