File Exchange

image thumbnail

Data-driven Control

version (450 KB) by Pravallika
Files used in webinar Data-driven Control conducted on 12 July 2012


Updated 01 Sep 2016

View License

Files used in webinar Data-driven Control: How to Design a Controller When a Plant Model is Not Available on 12 July 2012
Through this webinar and product demonstrations, you will learn how to design and implement a PID controller when a plant model is not available. Through a DC motor control example, you will learn how to:

- Apply input signals (voltage) to the motor and collect output (angular position) test data
- Identify and validate linear and nonlinear plant models using multiple data sets
- Design a linear controller based on the linear motor model and simulate its performance against the nonlinear model
- Implement and test the controller on a target hardware platform

Comments and Ratings (58)

Xiaolu Wang

@Marcio Figueiredo Garcia Have you fixed this problem?

hamza ali

Can this work without arduino IO package?
I have latest package given by matlab

I'm trying to simulate ... but I stopped at this error:
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
An error occurred while running the simulation and the simulation was terminated
Caused by:
Size mismatch for MATLAB expression 'int16'. Expected = 1x4 Actual = 1x1

Anyone have a solution?

Hello @Pravallika, Good morning!
Thank you for this beautiful work, very good! But I need to evolve to get my DC motor model because I've been confused with the links since its inception.

I'm trying to do this simulation to get the 12V DC motor model with built-in encoder and an H bridge L298. I'm using Matlab 2017b. Could you help me with a wiring diagram on the Arduino Mega 2560? Because I have not been able to simulate yet and in the case study document does not give details of connections, I still have many doubts. I also tried to run (EmbeddedOpenLoop) and just appeared on the matlab desktop (tout 1000x1double). I am grateful if you can help me because it will be very important for me to model my motors and I am realizing that this method is very good for doing this.

Hi Arthur, Good morning!
Were you able to do these tests to get the DC motor model? You could help me with a wiring plan as I have not got it yet and in the case study document it does not give details of connections. I'm using arduino UNO, an H L298 bridge and a simple motor with encoder. I'm grateful if you can help!


my serial communication between Arduino MEGA and PC is not working. I even tried to send the data that I received at the arduino but it would send many "255"s back to the PC and only a few right values. The data logged was, for example, [255, 255, 255, 46, 255, 255, ...] when I sent 46 from the PC to the MEGA and than from the MEGA to the PC back.

Did someone also had problems with the communication?

Thanks for the attention,

to someone who use L298N as motor driver, you guys need to use a 12v DC motor or any DC motor that have encoder,connect the motor power pins to the L298N power pins. The motor encoder pins labeled Hall VCC and Hall GND should be connected to the Arduino 5V and GND pins. Finally, connect motor encoder output channel CH.A to Arduino pin.

linh le

hi, i try to follow your project but i dont know the connection between arduino and motor driver. why you have so many wire connected to your arduino? i only have 3 wire connected to it. hope someone can clarify this for me. i am using L298N, and i connect pin 10 and 11 to In1 and In2 and the pot to pin A0. i hope someone can tell me the function of the extra wire there. thanks

hello ma'm, can u please provide me with the information of the motor-driver that was used in the circuit and its connections.

Marc Roth

When I try to build the matlab-function for the movemotor/closed loop interface i get the following mistake:

The variable 'packet' appears to be preallocated, but preallocation is not recommended here.

There are also other errors when I try to build the Matlab-Function. Maybe you can help me.
Marc Roth

Marc Roth

Guten Tag,
Ich habe gerade mit ihrem Kollegen vom Support gesprochen und er bat mich ihnen hier einen Kommentar zu hinterlassen.
Mein Problem ist, dass ich den Code für die Matlab-Funktionsblöcke nicht kompilieren kann um sie in Simulink zu nutzen. Vielleicht könnten sie mir weiterhelfen.
Mit freundlichen Grüßen
Marc Roth

reham shak

when I want to run the simulation this error appears
Failed to load library 'demoLibrary' referenced by 'Host_serial_final/Serial Motor Interface/10-bit to Angle'
could u help me to solve it?

when I want to run the simulation this error appears
Failed to load library 'demoLibrary' referenced by 'Host_serial_final/Serial Motor Interface/10-bit to Angle'
could u help me to solve it?


Hello Every one,
I have a problem in "EmbeddedOpenLoop" . When i try to run the model on target hardware it gives me this error

"Making simulation target "EmbeddedOpenLoop_sfun", ...

'EmbeddedOpenLoop_sfun.bat' is not recognized as an internal or external command,
operable program or batch file. " .

I am using R2013 ,


Hi Pravallika.
I have a few problems,
1. I am using a Uno board with Simulink 2013.
I need to transmit encoder data from arduino to simulink to plot for the system identification. I used your models and changes them slightly to my need.
I then used a S-function builder block to run my arduino using input voltage.
At this point, I am reading an analog data from pin 1 and trying to transmit it to serial. There are two problems
The read data has delay. Also, after some time it is a constant value (I guess it reads 0 or 1 only) I have to rebuild my arduino model and try again to work.
Please advise.

Thanks for your help!



Yes you can use the L298N but it will be better to use the sn75441one, it priovides more channel current.

I am not sure wrt potentiometer question.

I want to start doing this nice project but i need some help:
what type of motor driver should i use ? can i use Use a Arduino L298N Dual H-Bridge ?
and how or where do u place a potentiometer for 360 degree position feedback ?


Does anyone know why the second custom regressor is >0.7 and <-0.34. From what i think, is that from the plot of the z7(deadzone), the minimum voltage value is 0.7 that causes it to first increase in angle however for the maximum angle to decrease the minimum voltage was not -0.34 but -0.56 which occurred at time 38.9 i.e at time 38.9 the motor first experienced some decrease and at this time the voltage was -0.56V and not -0.34 V ?


@fai zal i dont have any coding for the communication. The external mode is easy to implement on the mega. Just configure the settings under tools>prepare to run, ensure that external mode is selected. Also ensure that external mode is selected next to the duration. Then click build model, after click connect to target and then click the play button.

fai zal

@Steve my error in Expected = 1x4 Actual = 1x0 Block Closed Loop Interface/Serial Motor Interface/Serial Interface to Arduino is solve now,,,please HELP me can you attach you communication between arduino and matlab (coding in matlab and coding in arduino),,

fai zal

@Steve i get this error when trying to run on the hardware not simulation, thanks for that advice.


@fai zal, you get this error when trying to run on the hardware or simulation? If it is hardware, i get the same when trying to use my UNO but i got it to work one time and from then on i use my mega and run the external mode program. I will look into your problem some more and see if I can get back to you.

fai zal

i can get very good graph in step response from this project but my timeout from this analysis will happen when this error appear Expected = 1x4 Actual = 1x0 Block Closed Loop Interface/Serial Motor Interface/Serial Interface to Arduino. when this error happen my analysis will stop immediately, please help me

fai zal

thanks @steve,,but why this error happen again error Expected = 1x4 Actual = 1x0 Block Closed Loop Interface/Serial Motor Interface/Serial Interface to Arduino


This simulink file is for use for arduino running on external mode and it is for real time testing on hardware after you have created your controller etc before.

I hope you all solve that error soon.

fai zal

@steve please attach that file that i could use to run in real time on the hardware using external mode,,,anyone can solve this error Expected = 1x4 Actual = 1x0 Block Closed Loop Interface/Serial Motor Interface/Serial Interface to Arduino (#84) ,,please


@yonas and @fai . I got that error too. What I did that seemed to work was I downloaded the embeded program first to my arduino and then I run the host model. the order made a difference. If you have a Arduino Mega I can send you the files that you could use to run in real time on the hardware using external mode, instead of the files from the Real Time folder provided that runs on your UNO.

fai zal

@yonas, @Ahmed mahlawy please help me if you found the solution about that problem thanks


Why did you choose those data sets : z7,z3 & z6 for use as the working data sets ?

i.e what is specific about those for use as the working data set as oppose to others ?

@yonas habte do you found any answer to demoLibrary/Utilities/10-bit to Angle problem?please inform me if you found the solution.. thank you.

yonas habte

Size mismatch for MATLAB expression 'int16'. Expected = 1x4 Actual = 1x0 Block Serial Motor Interface/Serial Interface to Arduino (#37) While executing: none

yonas habte

error loading demoLibrary/Utilities/10-bit to Angle in the Host_serial_final_MATLAB/Serial Motor Interface pleas help what is the problem


@Jason In my opinion its not necessary, you can use a simple H-bridge like a LM293.

@Steve I got two motors working using external mode on the mega. I will send you the simulink model.


What is the IC board the arduino is connected to? Is it off the shelf? can I buy one? Is it necessary?

Hello, by chance was anyone able to get two motors working with this model ? I am having some difficulty getting 2 to work on my Mega :(
Any direction or help will greatly be appreciated :)

@Isaiah, yea you should set back to zero but its not entirely necessary because we just want the response (pattern).
Yea you make the iddata set right after you have simulated each voltage command.


Hi all, Its me again. I just have a few small questions.

In the data acquisition category, when we send the test voltage signals, sometimes the resulting angle is not zero set initially so the angle will sometimes start from say 100 and drive to some angle dependent on the voltage level sent. Should i set the angle of the servo's horn to zero every time i send a new voltage signal ?

When creating the iddata sets, i saw the example you did on how to make z4. Do you make the iddata set right after you simulated "one" voltage command or is it done after you have sent all the voltage commands and received the corresponding angles ? In addition, the process to make another iddata is simply just the same as you did with z4 just using a different z(num) ?


@Pravallika I tried running mex -setup but i got back the same error when i tried to build. This is what i did

>mex -setup

Please verify your choices:

Compiler: Microsoft Visual C++ 2012
Location: C:\Program Files (x86)\Microsoft Visual Studio 11.0

Are these correct [y]/n? y

Warning: MEX-files generated using Microsoft Visual C++ 2012 require
that Microsoft Visual Studio 2012 run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.

Trying to update options file: C:\Users\Isaiah\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2013a\bin\win64\mexopts\msvc110opts.bat

Done . . .


Thanks Steve!

@Eric, Isaiah: Like Steve mentioned, running mex -setup to choose a compiler should help.

Also, you can get the real time pacer block from here:

Hey Eric, you should try running "mex -setup" and select a C Compiler. @Isaiah De Freitas, you should run mex -setup too in matlab command window. It will ask you a series of questions there for selection of a C-compiler. Hope you all get through. Cheers....

Excellent Tutorial

Hi, Excellent Tutorial. Unfortunately I can't get it to work for me. I am using 2013a. When i try to build the EmbededOpenLoop i get the following error >

"EmbeddedOpenLoop_sfun", ...

'EmbeddedOpenLoop_sfun.bat' is not recognized as an internal or external command,
operable program or batch file."

Also, when I try to build the Host_serial_final_Matlab i get the following error

"Level-2 MATLAB S-function 'msfun_realtime_pacer' does not exist."

Also i should note that I changed the file format from .mdl to .slx because it was recommended .


For both the "Open Loop" and "Host_serial_final_MATLAB" simulink systems after i set up the uno under the tools option and click run or build, I get the following errors "Unable to locate a C-compiler required by Stateflow and MATLAB Function blocks.
Use 'mex -setup' to select a supported C-compiler" reported by Simulink and "Model error" reported by simulink. I am using 2012b.
By the way, aid anyone get it to work on 2014a ?

saif malik

@pravallika Thankyou so much pravallika.. Appreciate it!


no problem, and thanks on behalf of saif.



@Isaiah: Thanks for explaining the connections! You should be able to follow any of the instructions that show up on google search to change the servo motor to continuous rotation as well.

@Ariful, Saif: To address the error -
1) Right click on the block it in the error: 'Host_serial_final_MATLAB/Serial Motor Interface'.
2) Click on Properties.
3) Go to the 'Callbacks' tab.
4) Click on 'StartFcn' in the list on the left pane.
5) Edit the commands in the pane on the right to reflect your COM port settings.


I hope both of you all receive a solution soon. Correct me if I am wrong but I think the connections are like:
First off remove the internal circuitry of the RC servo leaving only the wires connected to the DC motor and the wires coming from the pot. Connect the DC motor wires to the respective (one for forward and one lead for backward) PWM output pins ( i think in this case we used pins 10 and 11) of your motor shield/driver. Connect the wiper lead from the pot to an analog input pin ( in this case i think it is pin 0). Keep in mind also that we have to use an external power source for the motor driver.

saif malik

Hi! I have the same error..I am using matlab 2013a. If you somehow resolve it do let me know. Also plz tell me how have you physically connected the hardware as I cannot understand the connections in the pdf paper.
Plz reply asap..


Hi Thanks for this webniar. when i tried to work with this i am getting the following error fro the host serial model. i have successfully downloaded the model on arduino uno and my port is 6 for uno. please help me eliminate the error.
thanks again

Error evaluating 'StartFcn' callback of SubSystem block 'Host_serial_final_MATLAB/Serial Motor Interface'.

Open failed: Port: COM4 is not available. Available ports: COM3, COM6.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.


If I was to physically move the horn(arm) of the motor after a desired input angle was set, will the horn retract back to its desired angle?

- Yes. In fact, you might find it difficult to move the motor position since the controller will be acting against you.

Is this a hobby servo motor modified by removing the internal circuitry?

- Yes, we modified it for continuous rotation.


This is excellent work.
I have just two questions (anyone can answer) :

If I was to physically move the horn(arm) of the motor after a desired input angle was set, will the horn retract back to its desired angle?
Is this a hobby servo motor modified by removing the internal circuitry?


10-bit to Angle ?


@Mohamed: If you have confirmed that your model simulates slower than real-time, you can use other techniques to speed up your simulation such as Accelerator mode. You can then use one of the Pacer blocks on MATLAB Central to slow your simulation down to soft real-time. If this does not work for you, please feel free to contact our technical support who might be able to recommend an appropriate suggestion once they have more details pertaining to your application.

@phil: Thank you for your feedback. You are right, we could not use External mode on Arduino Uno as it is unsupported due to memory limitations of the board. Due to this, we created a host model that runs soft real-time on the PC that communicates with the application on the board. However, for Arduino Mega 2560 external mode is supported in MATLAB R2012b. You should be able to forgo the host model and collect your data via external mode.


Great Work ...
but, I need to decrease the sampling time to 0.0004 in order to identify a fast motor.
How can I do this without losing the real time.



This is a great webinar. It might be worth mentioning that you can not use this method of data collection in External mode. When using the arduino mega for example. You can not log signal data in external mode. Rather you should connect a regular scope and save its data to the workspace. (this is with R2012b)



Updated license

Added updated zip file with copyright info.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor