从MATLAB中的类将值导入工作空间

时间:2015-04-21 20:21:23

标签: matlab class import workspace

我对MATLAB有疑问。我有类文件,我可以通过简单地在我的脚本中定义问题参数来解决我的问题,并在MATLAB中调用一个类来解决我提供的参数问题。现在,由类计算得到的变量,如何将它们导入到worskspace中或在我的MATLAB脚本中显示它们而不改变类?

班级代码



classdef rankine
    % Rankine cycle class
    properties
        % Inlet properties
        mass_flow; % Mass flow of system (kg/s)
        Pboiler; % Boiler pressure (bar)
        superheat; % Boiler degrees superheat (C)
        isen_eff_turbine; % Turbine isentropic efficiency
        Pcond; % Condenser pressure (bar)
        subcooling; % Condenser subcooling
        
        % Calclated properties
        h_out_boiler; % Boiler outlet enthalpy (kJ/kg)
        s_out_boiler; % Boiler outlet entropy (kJ/kg-K)
        h_out_turbine; % Turbine outlet enthalpy (kJ/kg)
        s_out_turbine; % Turbine outlet entropy (kJ/kg-K)
        h_out_condenser; % Condenser outlet enthalpy (kJ/kg)
        s_out_condenser; % Condenser outlet entropy (kJ/kg-K)
        h_out_pump; % Pump outlet enthalpy (kJ/kg)
        s_out_pump; % Pump outlet entropy (kJ/kg-K)
        turbine_exh_quality; % Turbine exhaust quality
        T_boiler_out; % Boiler outlet temperature (C)
        T_turbine_out; % Turbine outlet temperature (C)
        T_condenser_out; % Condenser outlet temperature (C)
        T_pump_out; % Pump outlet temperature (C)
        Q_boiler; % Boiler Heat Rate (kW)
        W_turbine; % Turbine Work Rate (kW)
        Q_condenser; % Condenser Heat Rate (kW)
        W_pump; % Pump Work Rate (kW)
        Q_net; % Plant Net Heat Rate (kW)
        W_net; % Plant Net Work Rate (kW)
        plant_efficiency; % Plant Thermal Efficiency
        
    end
    
    methods % Methods for rankine class
        % Make constructor for the class
        function obj=rankine(mass_flow,Pboiler,superheat,isen_eff_turbine,Pcond,subcooling)
            
            % Enter system parameters
            obj.mass_flow=mass_flow;
            obj.Pboiler=Pboiler;
            obj.superheat=superheat;
            obj.isen_eff_turbine=isen_eff_turbine;
            obj.Pcond=Pcond;
            obj.subcooling=subcooling;
            
            % Calculate boiler outlet specific enthalpy and entropy
            if ~superheat % saturated vapor
                obj.T_boiler_out=XSteam('Tsat_p',Pboiler);
                obj.h_out_boiler=XSteam('hV_p',Pboiler);
                obj.s_out_boiler=XSteam('sV_p',Pboiler);
            else % Superheat
                obj.T_boiler_out=XSteam('Tsat_p',Pboiler)+superheat;
                obj.h_out_boiler=XSteam('h_pT',Pboiler,obj.T_boiler_out);
                obj.s_out_boiler=XSteam('s_pT',Pboiler,obj.T_boiler_out);
            end
            
            % Calculate turbine parameters
            h_out_s_turbine=XSteam('h_ps',Pcond,obj.s_out_boiler);
            obj.h_out_turbine=obj.h_out_boiler-isen_eff_turbine*(obj.h_out_boiler-h_out_s_turbine);
            obj.s_out_turbine=XSteam('s_ph',Pcond,obj.h_out_turbine);
            obj.turbine_exh_quality=XSteam('x_ph',Pcond,obj.h_out_turbine);
            obj.T_turbine_out=XSteam('Tsat_p',Pcond);
            obj.W_turbine=mass_flow*(obj.h_out_boiler-obj.h_out_turbine);
            
            % Calculate condenser parameters
            if ~subcooling % saturated liquid
                obj.h_out_condenser=XSteam('hL_p',Pcond);
                obj.s_out_condenser=XSteam('sL_p',Pcond);
                obj.T_condenser_out=obj.T_turbine_out;
                v=XSteam('vL_p',Pcond);
            else
                obj.T_condenser_out=obj.T_turbine_out-subcooling;
                obj.h_out_condenser=XSteam('h_pT',Pcond,obj.T_condenser_out);
                obj.s_out_condenser=XSteam('s_pT',Pcond,obj.T_condenser_out);
                v=XSteam('v_ph',Pcond,obj.h_out_condenser);
            end
            obj.Q_condenser=mass_flow*(obj.h_out_turbine-obj.h_out_condenser);
            
            % Calculate pump parameters
            obj.W_pump=(Pboiler-Pcond)*100*v*mass_flow;
            obj.h_out_pump=obj.h_out_condenser+(Pboiler-Pcond)*100*v;
            obj.s_out_pump=obj.s_out_condenser; % Isentropic case for now
            %obj.s_out_pump=XSteam('s_ph',Pboiler,obj.h_out_pump);
            obj.T_pump_out=XSteam('T_ph',Pboiler,obj.h_out_pump);
            
            % Calculate boiler heat rate
            obj.Q_boiler=(obj.h_out_boiler-obj.h_out_pump)*mass_flow;
            
            % Calcualte net work and heat rates
            obj.W_net=obj.W_turbine-obj.W_pump;
            obj.Q_net=obj.Q_boiler-obj.Q_condenser;
            obj.plant_efficiency=obj.W_net/obj.Q_boiler;
            
        end % End constructor
        
        function obj=update(obj,parameter_name,parameter_value)
            
            switch lower(parameter_name)
                case 'mass_flow'
                    obj.mass_flow=parameter_value;
                case 'pboiler'
                    obj.Pboiler=parameter_value;
                case 'superheat'
                    obj.superheat=parameter_value;
                case 'isen_eff_turbine'
                    obj.isen_eff_turbine=parameter_value;
                case 'pcond'
                    obj.Pcond=parameter_value;
                case 'subcooling'
                    obj.subcooling=parameter_value;
                otherwise
                    error('Parameter not modifiable.  Parameters to modify are mass_flow, Pboiler, superheat, isen_eff_turbine, Pcond, subcooling')
            end
            
            % Calculate boiler outlet specific enthalpy and entropy
            if ~obj.superheat % saturated vapor
                obj.T_boiler_out=XSteam('Tsat_p',obj.Pboiler);
                obj.h_out_boiler=XSteam('hV_p',obj.Pboiler);
                obj.s_out_boiler=XSteam('sV_p',obj.Pboiler);
            else % Superheat
                obj.T_boiler_out=XSteam('Tsat_p',obj.Pboiler)+obj.superheat;
                obj.h_out_boiler=XSteam('h_pT',obj.Pboiler,obj.T_boiler_out);
                obj.s_out_boiler=XSteam('s_pT',obj.Pboiler,obj.T_boiler_out);
            end
            
            % Calculate turbine parameters
            h_out_s_turbine=XSteam('h_ps',obj.Pcond,obj.s_out_boiler);
            obj.h_out_turbine=obj.h_out_boiler-obj.isen_eff_turbine*(obj.h_out_boiler-h_out_s_turbine);
            obj.s_out_turbine=XSteam('s_ph',obj.Pcond,obj.h_out_turbine);
            obj.turbine_exh_quality=XSteam('x_ph',obj.Pcond,obj.h_out_turbine);
            obj.T_turbine_out=XSteam('Tsat_p',obj.Pcond);
            obj.W_turbine=obj.mass_flow*(obj.h_out_boiler-obj.h_out_turbine);
            
            % Calculate condenser parameters
            if ~obj.subcooling % saturated liquid
                obj.h_out_condenser=XSteam('hL_p',obj.Pcond);
                obj.s_out_condenser=XSteam('sL_p',obj.Pcond);
                obj.T_condenser_out=obj.T_turbine_out;
                v=XSteam('vL_p',obj.Pcond);
            else
                obj.T_condenser_out=obj.T_turbine_out-obj.subcooling;
                obj.h_out_condenser=XSteam('h_pT',obj.Pcond,obj.T_condenser_out);
                obj.s_out_condenser=XSteam('s_pT',obj.Pcond,obj.T_condenser_out);
                v=XSteam('v_ph',obj.Pcond,obj.h_out_condenser);
            end
            obj.Q_condenser=obj.mass_flow*(obj.h_out_turbine-obj.h_out_condenser);
            
            % Calculate pump parameters
            obj.W_pump=(obj.Pboiler-obj.Pcond)*100*v*obj.mass_flow;
            obj.h_out_pump=obj.h_out_condenser+(obj.Pboiler-obj.Pcond)*100*v;
            obj.s_out_pump=XSteam('s_ph',obj.Pboiler,obj.h_out_pump);
            obj.T_pump_out=XSteam('T_ph',obj.Pboiler,obj.h_out_pump);
            
            % Calculate boiler heat rate
            obj.Q_boiler=(obj.h_out_boiler-obj.h_out_pump)*obj.mass_flow;
            
            % Calcualte net work and heat rates
            obj.W_net=obj.W_turbine-obj.W_pump;
            obj.Q_net=obj.Q_boiler-obj.Q_condenser;
            obj.plant_efficiency=obj.W_net/obj.Q_boiler;
            
        end % End update
        
        function plot_ts(obj) % Plots the t-s diagram of the process
            % Get the minimum and maximum pressures and temperatures
            Tmax=obj.T_boiler_out;
            Pmax=obj.Pboiler;
            Pmin=obj.Pcond;
            ts_diagram([Pmin Pmax],Tmax*1.5);
            title('T-s Diagram For Process')
            axesHandles=get(gcf,'Children');
            classHandles=handle(axesHandles);
            axes(classHandles(1))
            hold on
            
            % First draw the turbine process
            plot([obj.s_out_boiler obj.s_out_turbine],[obj.T_boiler_out obj.T_turbine_out],...
                'Linewidth',3,'Color','k')
     
            % Draw the condenser process
            sL=XSteam('sL_p',obj.Pcond); % Saturated liquid entropy
            s_line=[obj.s_out_turbine sL];
            T_line=[obj.T_turbine_out obj.T_turbine_out];
            if  (obj.subcooling~=0) % Subcooled
                s_line=[s_line obj.s_out_condenser];
                T_line=[T_line obj.T_condenser_out];
            end
            plot(s_line,T_line,'Linewidth',3)
            
            % Draw the pump process
            s_line=[obj.s_out_condenser obj.s_out_pump];
            T_line=[obj.T_condenser_out obj.T_pump_out];
            plot(s_line,T_line,'Linewidth',3,'Color',[0.5 0 0.5])
            
            % Draw the boiler process
            sL=XSteam('sL_p',obj.Pboiler);
            s_subcooled=linspace(obj.s_out_pump,sL,101);
            T_subcooled=arrayfun(@(s) XSteam('T_ps',obj.Pboiler,s), s_subcooled);
            sV=XSteam('sV_p',obj.Pboiler);
            TV=XSteam('Tsat_p',obj.Pboiler);
            s_line=[s_subcooled sV];
            T_line=[T_subcooled TV];
            if (obj.superheat>0)
                s_super=linspace(sV,obj.s_out_boiler,101);
                T_super=arrayfun(@(s) XSteam('T_ps',obj.Pboiler,s), s_super);
                s_line=[s_line s_super(2:end)];
                T_line=[T_line T_super(2:end)];
            end
            plot(s_line,T_line,'Linewidth',3,'Color','r')
            
            LH=legend('Turbine','Condenser','Pump','Boiler','Location','northwest');
            set(LH,'Color','w')
            
        end % End plot_ts
        
        function plot_mollier(obj)
            % Get extremities of operating points
            Phigh=obj.Pboiler;
            Plow=obj.Pcond;
            slow=obj.s_out_condenser;
            shigh=obj.s_out_boiler;
            hlow=obj.h_out_condenser;
            hhigh=obj.h_out_boiler;
            x_exhaust=obj.turbine_exh_quality;
            Thigh=obj.T_boiler_out;
            T_low=XSteam('T_ps',Phigh,shigh);
            
            pressures=[Plow/2 Plow Plow/4+Phigh/8 Plow/2+Phigh/4 Phigh/2 Phigh Phigh*1.5];
            temperatures=[Thigh-rem(Thigh,10)+10];
            entropies=[slow*0.8 shigh*1.2];
            qualities=min([0.7 x_exhaust-rem(x_exhaust,0.05)-0.05]):0.01:1;
            hrange=[hlow*0.8 hhigh*1.2];
            mollier(entropies,hrange,pressures,temperatures,qualities);
            title('Mollier diagram for process')
            axesHandles=get(gcf,'Children');
            classHandles=handle(axesHandles);
            axes(classHandles(1))
            hold on
            
            % Plot turbine line
            plot([obj.s_out_boiler obj.s_out_turbine],...
                 [obj.h_out_boiler obj.h_out_turbine],...
                 'Linewidth',3,'Color','k')
             
             % Plot condenser line
             scond=linspace(obj.s_out_turbine,obj.s_out_condenser,101);
             hcond=arrayfun(@(s) XSteam('h_ps',obj.Pcond,s),scond);
             plot(scond,hcond,'Linewidth',3,'Color','b')
             
             % Plot pump line
             spump=[obj.s_out_condenser obj.s_out_pump];
             hpump=[obj.h_out_condenser obj.h_out_pump];
             plot(spump,hpump,'Linewidth',3,'Color',[0.5 0 0.5])
             
             % Plot boiler line
             sboiler=linspace(obj.s_out_pump,obj.s_out_boiler,101);
             hboiler=arrayfun(@(s) XSteam('h_ps',obj.Pboiler,s),sboiler);
             plot(sboiler,hboiler,'Linewidth',3,'Color','r')
             
             LH=legend('Turbine','Condenser','Pump','Boiler','Location','northwest');
             set(LH,'Color','w')
             
        end % end plot_mollier
        
    end % End Methods
end % End rankine class




我的脚本调用它。



clc;
clear all;
close all;
import rankine

%Problem 1
mass_flow = 200;
Pboiler = 50;
superheat = 0;
isen_eff_turbine = 0.85;
Pcond = 1;
subcooling = 5;
problem1 = rankine(mass_flow, Pboiler, superheat, isen_eff_turbine, Pcond, subcooling)




Rankine类为每个步骤输出解决方案,例如



h_out_boiler
s_out_boiler
h_out_turbine
s_out_turbine




基本上,我想把Rankine类输出的变量放到MATLAB工作区而不修改rankine类。 感谢。

2 个答案:

答案 0 :(得分:2)

将以下内容添加到您的脚本中:

rankineOutput = struct(problem1) ;      %// convert the object into a structure

fldNames = fieldnames(rankineOutput) ;  %// get the field names of the structure

for iField = 1:numel(fldNames)
    varName = fldNames{iField} ;                            %// extract specific field name (just for the sake of clarity)
    assignin('base', varName , rankineOutput.(varName) ) ;  %// assign the value in the field to a variable with the same name in the base workspace
end

您可能会收到关于将对象转换为结构的警告,但您可以忽略它(它不会更改类的实现)。

这会将所有字段提取到基础工作区中的变量中。如果您只想要一个子集,则可以使用相同的方法,但在提取的字段中更具选择性。


取决于您的Matlab版本,您甚至可以通过直接在对象上调用它来避免转换为结构:

fldNames = fieldnames(problem1) ;  %// get the field names of the object

for iField = 1:numel(fldNames)
    varName = fldNames{iField} ;                       %// extract specific field name (just for the sake of clarity)
    assignin('base', varName , problem1.(varName) ) ;  %// assign the value in the field to a variable with the same name in the base workspace
end

答案 1 :(得分:1)

假设类构造函数是rankine(...)并且被称为rankine.m。将文件放入Matlab目录。然后,使用以下行来实例化它。

problem1 = rankine(mass_flow, Pboiler, superheat, isen_eff_turbine, Pcond, subcooling)

在该行之后,对象problem1现在位于您的工作区中。由于属性是公共属性,因此您可以使用.运算符直接访问它们。您也可以使用其他属性执行此操作。

problem1.h_out_boiler
problem1.s_out_boiler
problem1.h_out_turbine
problem1.s_out_turbine