Difference between revisions of "HANtune/MATLAB Interface"
Line 1: | Line 1: | ||
{{HANtune}}HANtune can be interfaced with MATLAB® (and by extension Simulink®). We can do this by using the MATLAB engine and calling it from a [[HANtune/Scripting|script]]. | {{HANtune}}HANtune can be interfaced with MATLAB® (and by extension Simulink®). We can do this by using the MATLAB engine and calling it from a [[HANtune/Scripting|script]]. | ||
− | The scripts needed to run this example can be found in HANtune\scripts\examples. These are: | + | The scripts needed to run this example can be found in HANtune\scripts\examples\MATLAB_Interface. These are: |
*ConnectToMatlab.py | *ConnectToMatlab.py | ||
*ReadSimulinkModel.py | *ReadSimulinkModel.py | ||
Line 9: | Line 9: | ||
==Connecting to the MATLAB Engine== | ==Connecting to the MATLAB Engine== | ||
+ | |||
+ | The firs thing we need to do is tell HANtune where to find the MATLAB engine. | ||
''Note: You will need to have installed a version of MATLAB® with the Java MATLAB engine.'' | ''Note: You will need to have installed a version of MATLAB® with the Java MATLAB engine.'' | ||
+ | |||
+ | Open ''ConnectToMatlab.py'' and change the following line to the location of your MATLAB engine: | ||
<nowiki>#Change this to your location of the MATLAB engine | <nowiki>#Change this to your location of the MATLAB engine |
Revision as of 09:08, 23 October 2019
HANtune can be interfaced with MATLAB® (and by extension Simulink®). We can do this by using the MATLAB engine and calling it from a script.
The scripts needed to run this example can be found in HANtune\scripts\examples\MATLAB_Interface. These are:
- ConnectToMatlab.py
- ReadSimulinkModel.py
- createDaqList.m
- getDaqListData.m
Contents
Connecting to the MATLAB Engine
The firs thing we need to do is tell HANtune where to find the MATLAB engine.
Note: You will need to have installed a version of MATLAB® with the Java MATLAB engine.
Open ConnectToMatlab.py and change the following line to the location of your MATLAB engine:
#Change this to your location of the MATLAB engine enginePath = "C:\Program Files\MATLAB\R2017b\extern\engines\java\jar\engine.jar"
#Add matlab engine to the classpath from datahandling import CurrentConfig CurrentConfig.getInstance().addSoftwareLibrary(enginePath)
#Import matlab engine from com.mathworks.engine import MatlabEngine #Initialize matlab engine engines = MatlabEngine.findMatlab() engine = MatlabEngine.connectMatlab(engines[0])
#Add engine as a global variable to all interpreters from nl.han.hantune.scripting import ScriptingManager ScriptingManager.getInstance().setGlobal("engine", engine)
#Redirect matlab output to HANtune console from java.lang import System from nl.han.hantune.scripting import Console System.setOut(Console.getGUI().getOut()) System.setErr(Console.getGUI().getErr())
Reading data from a running Simulink model
Set your model
#Change these to the name and location of your model modelName = 'MyModel' modelPath = 'C:\Users\Michiel Klifman\Desktop'
#Add folders to MATLAB path from java.lang import System scriptPath = System.getProperty("user.dir") + "\scripts\examples\MATLAB_Interface" engine.eval("addpath('" + scriptPath + "')") engine.eval("addpath('" + modelPath + "')")
Creating a daq list
#Create a daqlist from signals in the model result = engine.feval(2, 'createDaqList', modelName, 'signals') daqItemNames = result[0] daqList = result[1] daqSize = len(daqList)
#Create a signal for each item in the daq list signals = [] removeAllSignals() for name in daqItemNames: signal = createSignal(name) signals.append(signal) updateLayout() print 'DAQ list created with ' + str(daqSize) + ' items'
Reading
#Start the simulation engine.eval("set_param('" + modelName +"','SimulationCommand','start');") print 'Simulation started...'
#Read data from the model in Simulink and add it to the signals in HANtune while (True): data = engine.feval('getDaqListData', modelName, daqList, daqSize) if not data: break i = 0 for value in data: signals[i].setValueAndTime(value, 0) i += 1 print 'Simulation completed!'