0

Let's say I have a simulation set up in Simulink, which consists of three blocks: Input (From Workspace block), model calculation (S-function block) and results (Outport). In my class, I do a lot of preprocessing on the input data, which is not shown here. Then, I point the Input block to use the inputArray, which is a property of my class.

classdef SimulationClass < handle
    %SimulationClass Simulate process using model 
    properties
        inputArray double = ones(101, 11)
        modelFilename char
        simOpts struct
        simulationTime double
        simulationResultsArray double
    end

    methods
        function obj = SimulationClass(simulationTime)
            obj.simulationTime = simulationTime;
        end

        function prepareSimulation(obj, modelFilename)
            obj.modelFilename = modelFilename;
            open_system(obj.modelFilename);
            set_param([obj.modelFilename '/fromWorkspaceBlock'],...
                'VariableName', 'obj.inputArray');
            obj.simOpts = simset('SrcWorkspace', 'current');
        end

        function runSimulation(obj)
            [~, ~, obj.simulationResultsArray] = sim(obj.modelFilename,...
                obj.simulationTime, obj.simOpts);
        end
    end
end

I can easily start the simulation run from the command prompt.

mySim = SimulationClass(1:10);
mySim.prepareSimulation('mySimulinkSheet');
mySim.runSimulation;

But, if I play around with some settings (e.g., different solvers) and press Run in Simulink, it doesn't work, since the data is not in the base workspace. I.e., I lose the interactivity with Simulink.

Any ideas on how to resolve this issue? The only workaround I have figured out so far would be to write an additional method (like writePropertiesToBaseWorkspace) which would convert the classes properties into variables, using assignin().

winkmal
  • 622
  • 1
  • 6
  • 16
  • 1
    How else do you expect the workspace variables to exist without creating them in some script or otherwise? I'm not sure how this is very different from the question you linked... – Wolfie Feb 02 '18 at 12:10
  • Well, it would be easier to have Simulink use the object's workspace even in interactive mode. But as soon as I hit the *Run* button, it starts looking for the variables only in the base workspace... Or maybe there's a different kind of block I could use for the `input` part, instead of `From Workspace`!? – winkmal Feb 02 '18 at 14:40
  • 1
    You would need to use the workaround you mentioned to write your data to base workspace. When running interactively you do not have your workspace in class methods available to Simulink. – Navan Feb 02 '18 at 18:08

0 Answers0