Evaluating Method for multiple Objects, without using Eval

1 visualización (últimos 30 días)
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!

Respuesta aceptada

per isakson
per isakson el 6 de Dic. de 2020
Editada: per isakson el 6 de Dic. de 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

Más respuestas (1)

Ivan Lizat
Ivan Lizat el 6 de Dic. de 2020
I got it, the solution was, as you proposed, to use object arrays.
Thank you !

Categorías

Más información sobre Class File Organization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by