Generated Code

The following is matlab code generated by the CellML API from this CellML file. (Back to language selection)

The raw code is available.

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = mainFunction()
    % This is the "main function".  In Matlab, things work best if you rename this function to match the filename.
   [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel();
end

function [algebraicVariableCount] = getAlgebraicVariableCount() 
    % Used later when setting a global variable with the number of algebraic variables.
    % Note: This is not the "main method".  
    algebraicVariableCount =19;
end
% There are a total of 7 entries in each of the rate and state variable arrays.
% There are a total of 42 entries in the constant variable array.
%

function [VOI, STATES, ALGEBRAIC, CONSTANTS] = solveModel()
    % Create ALGEBRAIC of correct size
    global algebraicVariableCount;  algebraicVariableCount = getAlgebraicVariableCount();
    % Initialise constants and state variables
    [INIT_STATES, CONSTANTS] = initConsts;

    % Set timespan to solve over 
    tspan = [0, 10];

    % Set numerical accuracy options for ODE solver
    options = odeset('RelTol', 1e-06, 'AbsTol', 1e-06, 'MaxStep', 1);

    % Solve model with ODE solver
    [VOI, STATES] = ode15s(@(VOI, STATES)computeRates(VOI, STATES, CONSTANTS), tspan, INIT_STATES, options);

    % Compute algebraic variables
    [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS);
    ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI);

    % Plot state variables against variable of integration
    [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends();
    figure();
    plot(VOI, STATES);
    xlabel(LEGEND_VOI);
    l = legend(LEGEND_STATES);
    set(l,'Interpreter','none');
end

function [LEGEND_STATES, LEGEND_ALGEBRAIC, LEGEND_VOI, LEGEND_CONSTANTS] = createLegends()
    LEGEND_STATES = ''; LEGEND_ALGEBRAIC = ''; LEGEND_VOI = ''; LEGEND_CONSTANTS = '';
    LEGEND_VOI = strpad('time in component environment (s)');
    LEGEND_STATES(:,1) = strpad('F6P in component F6P (mM)');
    LEGEND_ALGEBRAIC(:,1) = strpad('V_hk in component V_hk (mM_per_s)');
    LEGEND_ALGEBRAIC(:,13) = strpad('V_pfk in component V_pfk (mM_per_s)');
    LEGEND_ALGEBRAIC(:,19) = strpad('V_pfk2 in component V_pfk2 (mM_per_s)');
    LEGEND_STATES(:,2) = strpad('F26P in component F26P (mM)');
    LEGEND_STATES(:,3) = strpad('GAP in component GAP (mM)');
    LEGEND_ALGEBRAIC(:,11) = strpad('V_pk in component V_pk (mM_per_s)');
    LEGEND_STATES(:,4) = strpad('PYR in component PYR (mM)');
    LEGEND_ALGEBRAIC(:,2) = strpad('V_ldh in component V_ldh (mM_per_s)');
    LEGEND_ALGEBRAIC(:,6) = strpad('V_op in component V_op (mM_per_s)');
    LEGEND_STATES(:,5) = strpad('LAC in component LAC (mM)');
    LEGEND_ALGEBRAIC(:,17) = strpad('V_lac in component V_lac (mM_per_s)');
    LEGEND_STATES(:,6) = strpad('ATP in component ATP (mM)');
    LEGEND_ALGEBRAIC(:,16) = strpad('V_ATPase in component V_ATPase (mM_per_s)');
    LEGEND_ALGEBRAIC(:,7) = strpad('V_ck in component V_ck (mM_per_s)');
    LEGEND_ALGEBRAIC(:,8) = strpad('dAMP_dATP in component dAMP_dATP (dimensionless)');
    LEGEND_STATES(:,7) = strpad('PCr in component PCr (mM)');
    LEGEND_CONSTANTS(:,1) = strpad('Vmax_hk in component V_hk (mM_per_s)');
    LEGEND_CONSTANTS(:,2) = strpad('Km_ATP_hk in component V_hk (mM)');
    LEGEND_CONSTANTS(:,3) = strpad('KI_F6P in component V_hk (mM)');
    LEGEND_CONSTANTS(:,4) = strpad('Vmax_pfk in component V_pfk (mM_per_s)');
    LEGEND_CONSTANTS(:,5) = strpad('Km_ATP_pfk in component V_pfk (mM)');
    LEGEND_CONSTANTS(:,6) = strpad('Km_F6P_pfk in component V_pfk (mM)');
    LEGEND_CONSTANTS(:,7) = strpad('Km_F26P_pfk in component V_pfk (mM)');
    LEGEND_ALGEBRAIC(:,12) = strpad('AMP_act in component AMP_act (dimensionless)');
    LEGEND_ALGEBRAIC(:,10) = strpad('ATP_inh in component ATP_inh (dimensionless)');
    LEGEND_CONSTANTS(:,8) = strpad('Vmaxf_pfk2 in component V_pfk2 (mM_per_s)');
    LEGEND_CONSTANTS(:,9) = strpad('Vmaxr_pfk2 in component V_pfk2 (mM_per_s)');
    LEGEND_CONSTANTS(:,10) = strpad('Km_ATP_pfk2 in component V_pfk2 (mM)');
    LEGEND_CONSTANTS(:,11) = strpad('Km_F6P_pfk2 in component V_pfk2 (mM)');
    LEGEND_CONSTANTS(:,12) = strpad('Km_F26P_pfk2 in component V_pfk2 (mM)');
    LEGEND_ALGEBRAIC(:,18) = strpad('AMP_pfk2 in component AMP_pfk2 (dimensionless)');
    LEGEND_CONSTANTS(:,13) = strpad('Vmax_pk in component V_pk (mM_per_s)');
    LEGEND_CONSTANTS(:,14) = strpad('Km_ADP_pk in component V_pk (mM)');
    LEGEND_CONSTANTS(:,15) = strpad('Km_GAP_pk in component V_pk (mM)');
    LEGEND_ALGEBRAIC(:,5) = strpad('ADP in component ADP (mM)');
    LEGEND_CONSTANTS(:,16) = strpad('Vmax_op in component V_op (mM_per_s)');
    LEGEND_CONSTANTS(:,17) = strpad('Km_ADP_op in component V_op (mM)');
    LEGEND_CONSTANTS(:,18) = strpad('Km_PYR_op in component V_op (mM)');
    LEGEND_CONSTANTS(:,19) = strpad('kf_ldh in component V_ldh (per_s)');
    LEGEND_CONSTANTS(:,20) = strpad('kr_ldh in component V_ldh (per_s)');
    LEGEND_CONSTANTS(:,21) = strpad('kf_ck in component V_ck (per_mM_s)');
    LEGEND_CONSTANTS(:,22) = strpad('kr_ck in component V_ck (per_mM_s)');
    LEGEND_ALGEBRAIC(:,3) = strpad('Cr in component Cr (mM)');
    LEGEND_CONSTANTS(:,23) = strpad('PCrtot in component Cr (mM)');
    LEGEND_CONSTANTS(:,24) = strpad('Vmax_ATPase in component V_ATPase (mM_per_s)');
    LEGEND_CONSTANTS(:,25) = strpad('Km_ATP in component V_ATPase (mM)');
    LEGEND_ALGEBRAIC(:,15) = strpad('v_stim in component v_stim (dimensionless)');
    LEGEND_CONSTANTS(:,26) = strpad('Vlac_0 in component V_lac (mM_per_s)');
    LEGEND_CONSTANTS(:,27) = strpad('K_LAC_eff in component V_lac (per_s)');
    LEGEND_CONSTANTS(:,28) = strpad('K_LAC in component V_lac (dimensionless)');
    LEGEND_ALGEBRAIC(:,4) = strpad('u in component ADP (dimensionless)');
    LEGEND_CONSTANTS(:,29) = strpad('ANP in component model_parameters (mM)');
    LEGEND_CONSTANTS(:,30) = strpad('Q_adk in component model_parameters (dimensionless)');
    LEGEND_ALGEBRAIC(:,9) = strpad('AMP in component AMP (mM)');
    LEGEND_CONSTANTS(:,31) = strpad('nATP in component ATP_inh (dimensionless)');
    LEGEND_CONSTANTS(:,32) = strpad('KI_ATP in component ATP_inh (mM)');
    LEGEND_CONSTANTS(:,33) = strpad('nAMP in component AMP_act (dimensionless)');
    LEGEND_CONSTANTS(:,34) = strpad('Ka_AMP in component AMP_act (mM)');
    LEGEND_ALGEBRAIC(:,14) = strpad('unitpulseSB in component v_stim (dimensionless)');
    LEGEND_CONSTANTS(:,35) = strpad('stim in component v_stim (dimensionless)');
    LEGEND_CONSTANTS(:,36) = strpad('to in component v_stim (s)');
    LEGEND_CONSTANTS(:,37) = strpad('tend in component v_stim (s)');
    LEGEND_CONSTANTS(:,38) = strpad('v1_n in component v_stim (dimensionless)');
    LEGEND_CONSTANTS(:,39) = strpad('v2_n in component v_stim (dimensionless)');
    LEGEND_CONSTANTS(:,40) = strpad('t_n_stim in component v_stim (s)');
    LEGEND_CONSTANTS(:,41) = strpad('Kamp_pfk2 in component AMP_pfk2 (mM)');
    LEGEND_CONSTANTS(:,42) = strpad('nh_amp in component AMP_pfk2 (dimensionless)');
    LEGEND_RATES(:,1) = strpad('d/dt F6P in component F6P (mM)');
    LEGEND_RATES(:,2) = strpad('d/dt F26P in component F26P (mM)');
    LEGEND_RATES(:,3) = strpad('d/dt GAP in component GAP (mM)');
    LEGEND_RATES(:,4) = strpad('d/dt PYR in component PYR (mM)');
    LEGEND_RATES(:,5) = strpad('d/dt LAC in component LAC (mM)');
    LEGEND_RATES(:,6) = strpad('d/dt ATP in component ATP (mM)');
    LEGEND_RATES(:,7) = strpad('d/dt PCr in component PCr (mM)');
    LEGEND_STATES  = LEGEND_STATES';
    LEGEND_ALGEBRAIC = LEGEND_ALGEBRAIC';
    LEGEND_RATES = LEGEND_RATES';
    LEGEND_CONSTANTS = LEGEND_CONSTANTS';
end

function [STATES, CONSTANTS] = initConsts()
    VOI = 0; CONSTANTS = []; STATES = []; ALGEBRAIC = [];
    STATES(:,1) = 0.2;
    STATES(:,2) = 0.001;
    STATES(:,3) = 0.0405;
    STATES(:,4) = 0.1;
    STATES(:,5) = 0.5;
    STATES(:,6) = 2.402;
    STATES(:,7) = 18.14;
    CONSTANTS(:,1) = 2.5;
    CONSTANTS(:,2) = 0.5;
    CONSTANTS(:,3) = 0.068;
    CONSTANTS(:,4) = 3.85;
    CONSTANTS(:,5) = 0.05;
    CONSTANTS(:,6) = 0.18;
    CONSTANTS(:,7) = 0.01;
    CONSTANTS(:,8) = 2e-04;
    CONSTANTS(:,9) = 1.036e-04;
    CONSTANTS(:,10) = 0.05;
    CONSTANTS(:,11) = 0.01;
    CONSTANTS(:,12) = 0.0001;
    CONSTANTS(:,13) = 5.0;
    CONSTANTS(:,14) = 0.005;
    CONSTANTS(:,15) = 0.4;
    CONSTANTS(:,16) = 1.0;
    CONSTANTS(:,17) = 0.005;
    CONSTANTS(:,18) = 0.5;
    CONSTANTS(:,19) = 12.5;
    CONSTANTS(:,20) = 2.5355;
    CONSTANTS(:,21) = 3.0;
    CONSTANTS(:,22) = 1.26;
    CONSTANTS(:,23) = 20.0;
    CONSTANTS(:,24) = 0.9355;
    CONSTANTS(:,25) = 0.5;
    CONSTANTS(:,26) = 0.355;
    CONSTANTS(:,27) = 0.71;
    CONSTANTS(:,28) = 0.641;
    CONSTANTS(:,29) = 2.51;
    CONSTANTS(:,30) = 0.92;
    CONSTANTS(:,31) = 0.4;
    CONSTANTS(:,32) = 1.0;
    CONSTANTS(:,33) = 0.5;
    CONSTANTS(:,34) = 0.05;
    CONSTANTS(:,35) = 1;
    CONSTANTS(:,36) = 50;
    CONSTANTS(:,37) = 200;
    CONSTANTS(:,38) = 0.5;
    CONSTANTS(:,39) = 0.0;
    CONSTANTS(:,40) = 2;
    CONSTANTS(:,41) = 0.005;
    CONSTANTS(:,42) = 2;
    if (isempty(STATES)), warning('Initial values for states not set');, end
end

function [RATES, ALGEBRAIC] = computeRates(VOI, STATES, CONSTANTS)
    global algebraicVariableCount;
    statesSize = size(STATES);
    statesColumnCount = statesSize(2);
    if ( statesColumnCount == 1)
        STATES = STATES';
        ALGEBRAIC = zeros(1, algebraicVariableCount);
        utilOnes = 1;
    else
        statesRowCount = statesSize(1);
        ALGEBRAIC = zeros(statesRowCount, algebraicVariableCount);
        RATES = zeros(statesRowCount, statesColumnCount);
        utilOnes = ones(statesRowCount, 1);
    end
    ALGEBRAIC(:,4) = power(CONSTANTS(:,30), 2.00000)+ 4.00000.*CONSTANTS(:,30).*(CONSTANTS(:,29)./STATES(:,6) - 1.00000);
    ALGEBRAIC(:,5) =  (STATES(:,6)./2.00000).*( - CONSTANTS(:,30)+power(ALGEBRAIC(:,4), 1.0 ./ 2));
    ALGEBRAIC(:,3) = CONSTANTS(:,23) - STATES(:,7);
    ALGEBRAIC(:,7) =  CONSTANTS(:,21).*STATES(:,7).*ALGEBRAIC(:,5) -  CONSTANTS(:,22).*ALGEBRAIC(:,3).*STATES(:,6);
    RATES(:,7) =  - ALGEBRAIC(:,7);
    ALGEBRAIC(:,10) = power((1.00000+ CONSTANTS(:,31).*(STATES(:,6)./CONSTANTS(:,32)))./(1.00000+STATES(:,6)./CONSTANTS(:,32)), 4.00000);
    ALGEBRAIC(:,11) =  CONSTANTS(:,13).*(STATES(:,3)./(STATES(:,3)+CONSTANTS(:,15))).*(ALGEBRAIC(:,5)./(ALGEBRAIC(:,5)+CONSTANTS(:,14))).*ALGEBRAIC(:,10);
    ALGEBRAIC(:,2) =  CONSTANTS(:,19).*STATES(:,4) -  CONSTANTS(:,20).*STATES(:,5);
    ALGEBRAIC(:,6) =  CONSTANTS(:,16).*(STATES(:,4)./(STATES(:,4)+CONSTANTS(:,18))).*(ALGEBRAIC(:,5)./(ALGEBRAIC(:,5)+CONSTANTS(:,17))).*(1.00000./(1.00000+ 0.100000.*(STATES(:,6)./ALGEBRAIC(:,5))));
    RATES(:,4) = ALGEBRAIC(:,11) - (ALGEBRAIC(:,6)+ALGEBRAIC(:,2));
    ALGEBRAIC(:,9) = CONSTANTS(:,29) - (STATES(:,6)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,12) = power((1.00000+ALGEBRAIC(:,9)./CONSTANTS(:,34))./(1.00000+ CONSTANTS(:,33).*(ALGEBRAIC(:,9)./CONSTANTS(:,34))), 4.00000);
    ALGEBRAIC(:,13) =  CONSTANTS(:,4).*(STATES(:,1)./(STATES(:,1)+CONSTANTS(:,6))).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,5))).*(STATES(:,2)./(STATES(:,2)+CONSTANTS(:,7))).*ALGEBRAIC(:,10).*ALGEBRAIC(:,12);
    RATES(:,3) =  2.00000.*ALGEBRAIC(:,13) - ALGEBRAIC(:,11);
    ALGEBRAIC(:,14) = piecewise({VOI>=CONSTANTS(:,36)&VOI<=CONSTANTS(:,36)+CONSTANTS(:,37), 1.00000 }, 0.00000);
    ALGEBRAIC(:,15) =  CONSTANTS(:,35).*(CONSTANTS(:,38)+ CONSTANTS(:,39).*((VOI - CONSTANTS(:,36))./CONSTANTS(:,40)).*exp( - ( (VOI - CONSTANTS(:,36)).*(ALGEBRAIC(:,14)./CONSTANTS(:,40))))).*ALGEBRAIC(:,14);
    ALGEBRAIC(:,17) =  CONSTANTS(:,26).*(1.00000+ ALGEBRAIC(:,15).*CONSTANTS(:,28)) -  CONSTANTS(:,27).*STATES(:,5);
    RATES(:,5) =  2.25000.*ALGEBRAIC(:,2)+ALGEBRAIC(:,17);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,2))).*power(1.00000+power(STATES(:,1)./CONSTANTS(:,3), 4.00000), -1.00000);
    ALGEBRAIC(:,18) = power(ALGEBRAIC(:,9)./CONSTANTS(:,41), CONSTANTS(:,42))./(1.00000+power(ALGEBRAIC(:,9)./CONSTANTS(:,41), CONSTANTS(:,42)));
    ALGEBRAIC(:,19) =  CONSTANTS(:,8).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,10))).*(STATES(:,1)./(STATES(:,1)+CONSTANTS(:,11))).*ALGEBRAIC(:,18) -  CONSTANTS(:,9).*(STATES(:,2)./(STATES(:,2)+CONSTANTS(:,12)));
    RATES(:,1) = ALGEBRAIC(:,1) - (ALGEBRAIC(:,13) - ALGEBRAIC(:,19));
    RATES(:,2) = ALGEBRAIC(:,19);
    ALGEBRAIC(:,16) =  CONSTANTS(:,24).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,25))).*(1.00000+ALGEBRAIC(:,15));
    ALGEBRAIC(:,8) =  - 1.00000+CONSTANTS(:,30)./2.00000+ - ( 0.500000.*power(ALGEBRAIC(:,4), 1.0 ./ 2))+ CONSTANTS(:,30).*(CONSTANTS(:,29)./( STATES(:,6).*power(ALGEBRAIC(:,4), 1.0 ./ 2)));
    RATES(:,6) =  (( 2.00000.*ALGEBRAIC(:,11)+ 15.0000.*ALGEBRAIC(:,6)+ALGEBRAIC(:,7)) - (ALGEBRAIC(:,1)+ALGEBRAIC(:,13)+ALGEBRAIC(:,19)+ALGEBRAIC(:,16))).*power(1.00000 - ALGEBRAIC(:,8), -1.00000);
   RATES = RATES';
end

% Calculate algebraic variables
function ALGEBRAIC = computeAlgebraic(ALGEBRAIC, CONSTANTS, STATES, VOI)
    statesSize = size(STATES);
    statesColumnCount = statesSize(2);
    if ( statesColumnCount == 1)
        STATES = STATES';
        utilOnes = 1;
    else
        statesRowCount = statesSize(1);
        utilOnes = ones(statesRowCount, 1);
    end
    ALGEBRAIC(:,4) = power(CONSTANTS(:,30), 2.00000)+ 4.00000.*CONSTANTS(:,30).*(CONSTANTS(:,29)./STATES(:,6) - 1.00000);
    ALGEBRAIC(:,5) =  (STATES(:,6)./2.00000).*( - CONSTANTS(:,30)+power(ALGEBRAIC(:,4), 1.0 ./ 2));
    ALGEBRAIC(:,3) = CONSTANTS(:,23) - STATES(:,7);
    ALGEBRAIC(:,7) =  CONSTANTS(:,21).*STATES(:,7).*ALGEBRAIC(:,5) -  CONSTANTS(:,22).*ALGEBRAIC(:,3).*STATES(:,6);
    ALGEBRAIC(:,10) = power((1.00000+ CONSTANTS(:,31).*(STATES(:,6)./CONSTANTS(:,32)))./(1.00000+STATES(:,6)./CONSTANTS(:,32)), 4.00000);
    ALGEBRAIC(:,11) =  CONSTANTS(:,13).*(STATES(:,3)./(STATES(:,3)+CONSTANTS(:,15))).*(ALGEBRAIC(:,5)./(ALGEBRAIC(:,5)+CONSTANTS(:,14))).*ALGEBRAIC(:,10);
    ALGEBRAIC(:,2) =  CONSTANTS(:,19).*STATES(:,4) -  CONSTANTS(:,20).*STATES(:,5);
    ALGEBRAIC(:,6) =  CONSTANTS(:,16).*(STATES(:,4)./(STATES(:,4)+CONSTANTS(:,18))).*(ALGEBRAIC(:,5)./(ALGEBRAIC(:,5)+CONSTANTS(:,17))).*(1.00000./(1.00000+ 0.100000.*(STATES(:,6)./ALGEBRAIC(:,5))));
    ALGEBRAIC(:,9) = CONSTANTS(:,29) - (STATES(:,6)+ALGEBRAIC(:,5));
    ALGEBRAIC(:,12) = power((1.00000+ALGEBRAIC(:,9)./CONSTANTS(:,34))./(1.00000+ CONSTANTS(:,33).*(ALGEBRAIC(:,9)./CONSTANTS(:,34))), 4.00000);
    ALGEBRAIC(:,13) =  CONSTANTS(:,4).*(STATES(:,1)./(STATES(:,1)+CONSTANTS(:,6))).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,5))).*(STATES(:,2)./(STATES(:,2)+CONSTANTS(:,7))).*ALGEBRAIC(:,10).*ALGEBRAIC(:,12);
    ALGEBRAIC(:,14) = piecewise({VOI>=CONSTANTS(:,36)&VOI<=CONSTANTS(:,36)+CONSTANTS(:,37), 1.00000 }, 0.00000);
    ALGEBRAIC(:,15) =  CONSTANTS(:,35).*(CONSTANTS(:,38)+ CONSTANTS(:,39).*((VOI - CONSTANTS(:,36))./CONSTANTS(:,40)).*exp( - ( (VOI - CONSTANTS(:,36)).*(ALGEBRAIC(:,14)./CONSTANTS(:,40))))).*ALGEBRAIC(:,14);
    ALGEBRAIC(:,17) =  CONSTANTS(:,26).*(1.00000+ ALGEBRAIC(:,15).*CONSTANTS(:,28)) -  CONSTANTS(:,27).*STATES(:,5);
    ALGEBRAIC(:,1) =  CONSTANTS(:,1).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,2))).*power(1.00000+power(STATES(:,1)./CONSTANTS(:,3), 4.00000), -1.00000);
    ALGEBRAIC(:,18) = power(ALGEBRAIC(:,9)./CONSTANTS(:,41), CONSTANTS(:,42))./(1.00000+power(ALGEBRAIC(:,9)./CONSTANTS(:,41), CONSTANTS(:,42)));
    ALGEBRAIC(:,19) =  CONSTANTS(:,8).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,10))).*(STATES(:,1)./(STATES(:,1)+CONSTANTS(:,11))).*ALGEBRAIC(:,18) -  CONSTANTS(:,9).*(STATES(:,2)./(STATES(:,2)+CONSTANTS(:,12)));
    ALGEBRAIC(:,16) =  CONSTANTS(:,24).*(STATES(:,6)./(STATES(:,6)+CONSTANTS(:,25))).*(1.00000+ALGEBRAIC(:,15));
    ALGEBRAIC(:,8) =  - 1.00000+CONSTANTS(:,30)./2.00000+ - ( 0.500000.*power(ALGEBRAIC(:,4), 1.0 ./ 2))+ CONSTANTS(:,30).*(CONSTANTS(:,29)./( STATES(:,6).*power(ALGEBRAIC(:,4), 1.0 ./ 2)));
end

% Compute result of a piecewise function
function x = piecewise(cases, default)
    set = [0];
    for i = 1:2:length(cases)
        if (length(cases{i+1}) == 1)
            x(cases{i} & ~set,:) = cases{i+1};
        else
            x(cases{i} & ~set,:) = cases{i+1}(cases{i} & ~set);
        end
        set = set | cases{i};
        if(set), break, end
    end
    if (length(default) == 1)
        x(~set,:) = default;
    else
        x(~set,:) = default(~set);
    end
end

% Pad out or shorten strings to a set length
function strout = strpad(strin)
    req_length = 160;
    insize = size(strin,2);
    if insize > req_length
        strout = strin(1:req_length);
    else
        strout = [strin, blanks(req_length - insize)];
    end
end