Search code examples
matlabclassimportworkspace

Importing values into workspace from a class in MATLAB


I have a question regarding MATLAB. I have class file where I can solve my problem with by simply defining the problem parameters in my script and call on a class in MATLAB to solve the problem with parameters that I provide. Now, the resulting variables that are computed by the class, how can I import them into the worskspace or display them within my MATLAB script without altering the class?

Class code

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

My script calling onto it.

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)

The rankine class outputs solutions for each step such as

h_out_boiler
s_out_boiler
h_out_turbine
s_out_turbine

Basically, I would like to take the variables the rankine class outputs and put them into the MATLAB workspace without modifying the rankine class. Thanks.


Solution

  • Add the following to your script:

    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
    

    You might get a warning about the conversion of the object into structure but you can ignore it (it will not change the implementation of the class).

    This will extract all the fields into variables in the base workspace. If you only want a subset you can use the same method but be more selective in which field you extract.


    depending on your version of Matlab, you can even avoid the conversion to structure by calling the same directly on the object:

    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