main content

deploy app with live simulation results of lorenz system -凯发k8网页登录

this example shows how to develop an app that uses callbacks for simulation inputs and outputs to view the simulation of a simulink® model of the lorenz system. you can then deploy the app with simulink compiler™.

open the lorenz system app

this example uses a simulink model of the lorenz system, an app that is created in the matlab® app designer that simulates the model with different input and output values. to learn more about how to create an app using the app designer, see . to open the app, type the following at the matlab command window.

lorenzsystemapp

app details

open the lorenzsystemapp.mlapp file. you can view the code written to create this app in the code view section of app sesigner. the essential part of building this app is the behavior of the simulate button. it has the following salient parts: creating the simulationinput object, configuring it for deployment, using simulation callbacks to read the output port data and plot the data at each time step. these three functions allow you to see the live results of the simulation in the deployed app.

create the simulink.simulationinput object

in the function createsimulationinput, define an empty simulink.simulationinput object for the model. use this simulink.simulationinput object to set simulation callbacks and variables for the model.

the simulation callback functions are used to register the callbacks. the simulink.compiler.setpoststepfcn function registers a callback that is invoked after every simulation step. the simulink.compiler.setexternalouputsfcn registers a callback that dynamically processes the values for every output port at root level of a model during simulation.

use the setvariable method of the simulink.simulationinput object to provide the parameter values to the app. values for the simulation are obtained from the edit fields of the ui of the app. to deploy the app, use the simulink.compiler.configurefordeployment function. (comment the line of code that calls simulink.compiler.configurefordeployment function for faster debugging.)

function siminp = createsimulationinput(app)
            % create an empty simulationinput object
            siminp = simulink.simulationinput('lorenzsystemmodel');
            
            siminp = simulink.compiler.setsimulationstatuschangefcn(siminp, ...
                @(simstatus) app.simstatuschangedfcn(simstatus));
            % poststepfcn is used to update plots
            siminp = simulink.compiler.setpoststepfcn(siminp, ...
                @(simtime) app.poststepfcn(simtime), ...
                'decimation',app.poststepfcndecimation);
            
            % load the parameters values from the ui edit fields
            siminp = siminp.setvariable('rho',app.rhouic.value);
            siminp = siminp.setvariable('beta',app.betauic.value);
            siminp = siminp.setvariable('sigma',app.sigmauic.value);
            siminp = siminp.setvariable('x0',app.x0uic.value);
            siminp = siminp.setvariable('y0',app.y0uic.value);
            siminp = siminp.setvariable('z0',app.z0uic.value);
            
            % configure siminp for deployment
            siminp = simulink.compiler.configurefordeployment(siminp);
        end % createsimulationinput

simulation callback functions

the simulation callback functions register callbacks that allow you to read values from the output ports and to write values to the root input ports. these functions register callbacks at every simulation time step, which allows you to view live results of the simulation.

the poststepfcn callback

the poststepfcn callback function is invoked after every simulation step. the time argument is the time for the previous simulation step. the poststepfcn function obtains the cached outport block values for every simtime and uses those values to the plot the cached values at simulation time.

function poststepfcn(app, simtime)
            app.simtime.text = sprintf('%0.5g',simtime); % 0.1234e 56
            wctime = toc(app.wallclocktimeatsimstart);
            app.simpace.text = sprintf('%7.2f', simtime/wctime); % 1234.56
            app.adjustpoststepfcndecimation();
            simout = simulink.compiler.getsimulationoutput(app.modelname);
            ts = simulink.compiler.internal.extracttimeseriesfromdataset(simout.yout);
            xv = []; yv = []; zv = [];
            for its = 1:length(ts)
                idx = find(ts{its}.time >= app.simtimeatlastpoststep);
                switch ts{its}.name
                    case 'x'
                        xv = ts{its}.data(idx);
                    case 'y'
                        yv = ts{its}.data(idx);
                    case 'z'
                        zv = ts{its}.data(idx);
                end
            end
            addpoints(app.hline,xv,yv,zv);
            [xv, yv, zv] = getpoints(app.hline);
            set(app.htail,'xdata',xv(1),'ydata',yv(1),'zdata',zv(1));
            set(app.hhead,'xdata',xv(end),'ydata',yv(end),'zdata',zv(end));
            drawnow limitrate;
            app.simtimeatlastpoststep = simtime;
end

test in app designer

before deploying the application, ensure that the app runs in the app designer. click simulate to verify that the application works by simulating the model for different values.

compile app for deployment

you can use the app designer to compile and deploy the app. you can also use the deploytool function. for more information on compiling and deploying with the app designer, see , and .

to compile the app in this example, use the mcc command followed by the app name.

mcc -m lorenzsystemapp

see also

| | | | | | | |

related topics

网站地图