Matlab/Simulink LookupTable with Workspace vectors - matlab

I am new with Simulink and I am struggling with the Dynamic Lookup Table (inputs : x, xadta, ydata; output: y).
I have several 2D vectors (xdata and ydata) stored in my Workspace and I would like to use them in Simulink in a Dynamic Lookup Table to return a value (y) depending on another variable in Simulink (x).
If I understand how it works I have first to convert my 2D vectors in structures (time,values,dimensions) to be read in Simulink ?
So I did it this way but I got an error :
vector.time = xdata; % dimension 1x100
vector.signals.values = ydata; % dimension 1x100
vector.signals.dimensions = [1 100];
save('vector.mat','vector')
Error
"The last dimension of each
'signals.values' field must be the same as the number of rows in the 'time' field."
Besides I am not sure that what I am trying to do is appropriate... I use the xdata of my vectors/structures as "time" in the structures to get my vectors readable in Simulink. But I do not think it should have anything to do with time notion. I just want the Dynamic Lookup Table to return the "ydata" value of the vector/structure corresponding to the value of "x"="xdata". Only "x" change with time in the Simulation.

It looks like you should just be using the 1D Lookup Table, with your xdata and ydata variables (defined in your MATLAB Workspace) used as the block parameters.
As to the error you're getting, it seems to be related to using the From File block (which it looks like you're using to get data into your model) not the look-up table itself. To get that to work, define your time vector as a column vector, not a row vector as you've done, and think of each row as a different time point.
At each time point, you'll get a different signal value. This is like a look-up table itself (looking up the signal value for each different time value), but doesn't sound like what you are really wanting to achieve.

Related

Apply calculation to each element simultaneously in an array in matlab using a function (Version 2013b)

I have a matrix (type:double) of size 106 x 103. The matrix represents European gridded temperature data for one timestep (a day).
For every day, I want to calculate the degree days (thermal time) for a species, based on the temperature recorded for each 'cell' (i,j element) in the matrix using a formula that I have coded in Matlab based on a sinewave approach.
So, ultimately, what I want to do is being able to apply a calculation to my matrix, that will provide individual output for each grid cell (i,j element) dependent on the temperature data that is recorded there.
I could do this with a loop, but I have to accumulate these degree days for multiple years, so I would prefer to find a way of applying the calculation to each element in a daily matrix simultaneously (and then looping through the days (matrices)).
From what I have read, you can use a cellfun if your matrix is a cell array (mine is not).
Then I was also looking at the bsxfun option, but it seems like the functions are just standard functions..like mean, max etc.
So now, I'm looking at using arrayfun in conjunction with a function I create from my algorithm to calculate degree days.
I have been trying to write a test function but Matlab keeps throwing up the same error:
I type:
function output=degreedays(x)
and Matlab throws back:
Error: Function definitions are not permitted in this context.
Can someone tell me what I'm doing wrong? Why is it not accepting the declaration of the function name?
MATLAB does not allow you to define named functions like this at the command line. You need to place your function definition in a file. MATLAB then can call that function by the name of the file - so in your case, put your function definition in a file called degreedays.m.
See the doc for more: https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html .

Input Parameters of a 'From Workspace' Block in Simulink

I have a .mat file which has a structure loaded into the Workspace. I have created a simulink model and want to Import the signals from the Workspace. What should be the Input value for the Data Parameter of the 'From Workspace' block. The Name of the structure is Measurements, the Signal Name is B_cal and it has further elements as time,name, Units and value. I know that the structures can be accessed by somewhat like this command :
Measurements.B_cal.value
But i am unable to set the Input Parameters. Could anyone help me with this ?
There are some limitation to use structures through the FromWorkspace block:
A MATLAB expression that evaluates to one of the following:
A MATLAB timeseries object
A structure of MATLAB timeseries objects
A two-dimensional matrix:
The first element of each matrix row is a time stamp.
The rest of each row is a scalar or vector of signal values.
A structure, with or without time, which contains:
1) A signals.values field, which contains a vector of signal values
2) An optional signals.dimensions array, which contains the dimensions of the signal
3) An optional time vector, which contains time stamps
More useful information you can find in help.
So in your case you can use different methods. I'll give some examples:
1) define your struct in necessary format:
t = (1:10)'; %'
v = [6 9 3 1 7 0 7 3 8 1]'; %'
measure.time = t;
measure.signals.values = v;
Important moment here: t andv must be a columns! rows will not work!
If you need to use several rows of data use multidimensional v and add
measure.signals.dimentions = size(v,2);
2) You can see ths time field is an optional. If you do not have it you need to set Sample time in block other than 0 and, clear Interpolate Data, Set Form output after final data value by to a value other than Extrapolation. Furthermore, you need to define time field:
mystruct.time = [];
3) If you don't want to change your structure, you can use next:
t = (1:10)'; %'
and set this in Data of FromWorkspace block: [t, Measurements.B_cal.value].
4) There are some useful methods: use timeseries or just matrix. But it's not really your case if you need to use your structure.

Multiexperiments in iddata function from Matlab's system ID toolbox

I am trying to evaluate with iddata (INFO) in matlab, a number of N_E experiments.
I already computed and have as cell arrays of size 1xN_E the outputs and inputs, y and u respectively. Every entry of the cell arrays y and u is a vector of length N=316 (SISO system). For the sake of correctness, period is also a cell array of size 1xN_E, with the period in every entry.
Using the command:
data = iddata(y,u,period);
doesn't produce the expected averaged data-set. Instead, it is handled as a 361x361MIMO system (!).
I've already tried transposing, without results.
data = iddata(y.',u.',period.');
Does someone know why this happens, and how can I produce the desired multi-experiment data-set?
P.S. the documentation I read is for Matlab R2014b, and I am running R2013b. Does someone know if this was not supported in my edition? Or how can I find out?
Actually, the Matlab documentation provides an answer to my question.
The function iddata is very strict regarding how the dimension of output y, input u and period period are defined.
Defining 1xN_experiments cell arrays for y,u and period (note: same size for all!; also N_experimentsx1 won't be recognized by iddata) and then using iddata:
data = iddata(y,u,period);
gives the desired iddata structure.
Note all vectors within y and u must be of same length(!)

Loading data to simulink (Invalid matrix-format)

When I'm trying load data, from Matlab to Simulink, I get this error:
Error using TSFPnew (line 191)
Invalid matrix-format variable specified as workspace input in 'modelTSFP/From Workspace5'. The matrix
must have two dimensions and at least two columns. Complex signals of any data type and non-double
real signals must be in structure format. The first column must contain time values and the remaining
columns the data values. Matrix values cannot be Inf or NaN.
I have very simple model (I know, it be easier to do this computation on Matlab, but this is only fragment of my model):
All data have these same dimension 1x144:
Why I can't just load it to the Simulink space?
The error message is pretty self-explanatory: the data in the From Workspace block represents a time dependent variable so if you are using an array, the first column of the array must be the time values and the second (or more) columns the corresponding data points. Check the documentation for more details. Your data appears to be only vectors, where is the corresponding time data for your values?
If you want a parameter (that doesn't vary with time), then don't use a From Workspace block, use a Constant block instead.

MATLAB/Simulink - programmatically supply multiple external inputs

I have the following Simulink model:
I would like to externally provide inputs u[k] and y[k], i.e., I will be running simulations via MATLAB command line. I found previously that I could set the [LoadExternalInput and ExternalInput][3] options, and they default to the vector [t u].
But my u[k] and y[k] are vectors, and it looks like the ExternalInput can only specify one vector. So each row of [t u] is the value of the entire vector u at time t.
The sizes of u[k] and y[k] in my model here are not necessarily known ahead of time. Is there a way to pass in these vectors (as structs, perhaps)?
From Importing Data to Root-Level Input Ports I've found that I could do something like
sim('myModel', 'LoadExternalInput', 'on', 'ExternalInput', 'u, y');
where u and y are structures with fields time, signals.values, and signals.dimensions; each row of signals.values is a vector corresponding to a n element of time. signals.dimensions is the dimension of signals.values. I have to manually set the In1 ports to expect the same dimension as u.signals.values (well, I can of course do it programatically before hand..):
Note You must set the Port dimensions parameter of the Inport or the Trigger block to be the same value as the dimensions field of the corresponding input structure. If the values differ, an error message is displayed when you try to simulate the model.
(from "Importing Data Structures to a Root-Level Input Port")
What's the point of setting signals.dimensions if I have to set the dimension on the In1 block manually anyhow? Anyway that might have to be how I do this: just examine u and y before running the simulation, then setting the Inblock properties (programatically, of course) to expect vectors of that length.
I am still hoping there is a more elegant solution for this.