Working with time-series data in a Matlab Function in Simulink

90 views (last 30 days)
Hi,
I've collected several data from 2 sensors outside Matlab enrivonment, i've loaded them in a Matlab file through a load operation once they were stored in a .mat file. Let's call these two arrays "a" and "b". The sensors collected 18000 data each, working 30 mins, this means they have collected 10 values per second. I have converted the arrays in time-series using those lines:
Ts=0.1; %sampling time
timevals=[0:Ts:1799];
a_=timeseries(a,timevals);
b_=timeseries(b,timevals);
Then, i've uploaded them in Simulink through two "from workspace" blocks, i've computed their difference, let's call it "error" and then i have to work with a dynamic PI controller, this means that after the sum block that i used to compute the difference, there are two branches where this difference, the error, enters. I just have to implement a very simple function (my idea is to use two different Matlab Function Blocks, one for each branch), if the error is bigger than a quantity, the proportional action will lead to y=error*Kp, if the error is smaller or equal than this quantity, the action is y=error*Kp/100. Same for integral action. This sees to be an easy task but once i try, i get so many errors that i don't know from which one i should begin... the question is: how to deal with time series inside a Matlab Function block? I'm thinking there is a conceptual error i perform, so i'm looking for some advice. This would be very appreciated, thanks!!
Some of the errors I get:
1) Errors occurred during parsing of MATLAB function 'MATLAB Function1'
2) Undefined function or variable 'Kp' -> i've definied it both in worspace and in matlab function but i continue getting this error...
3) Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
4) Undefined function or variable 'y'. The first assignment to a local variable determines its class.
PS. I've also computed the two results easily in Matlab, getting the two arrays y and y1 (for proportional part and integral part). Could it be good to convert them in time-series and plug them in the Simulink model? In this way i will avoid performing the function in Simulink, but in this case i only find input time-series block, so i don't know how to put them in the middle of a simulink model. Simulink is really needed so I can't avoid it and just writing a Matlab script because the future operations are related to integrators and rate limiters that would be very complex without Simulink...

Answers (1)

MULI
MULI on 28 Apr 2024
Edited: MULI on 28 Apr 2024
Hi Luigi,
I understand that you are facing issue in handling timeseries data and performing required tasks on that data.
You may try these following suggestions in handling this data:
  • While importing the data of time series make sure you are entering the variable which has value. If ‘a’ is time series variable a.Data' has value.
  • Ensure parameter ‘Kp’ properly defined in MATLAB function block. Either you can define or pass it as an argument.
  • Make sure the Simulink model uses fixed time step solver with sample time that matches your data. This ensures an equal number of error samples and output data similar to input data.
Here I am attaching the Simulink model which demonstrates the implementation with some random data.
Random timeseries data generation for that model:
t=0:0.1:10;
a_data=sin(t);
a_timeseries=timeseries(a_data,t);
b_data=cos(t);
b_timeseries=timeseries(b_data,t);
You may refer to thses documenation links for more information on handling time series data
Hope this answers your query!

Products


Release

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!