Loading the data for a Simulink Lookup Table from a file

13,670

If I understand correctly, it sounds like you want to have a Lookup Table block with index and table values that can be dynamically updated during the course of the simulation. I believe you can do this using a From File block, a Demux block, and a Lookup Table Dynamic block. Let's say you have a .mat file containing an array of the following form:

[  time_1   time_2   time_3 ...;  %# Time stamps
 index1_1 index1_2 index1_3 ...;  %# Index 1 for all time stamps
 index2_1 index2_2 index2_3 ...;  %# Index 2 for all time stamps
 ...
 indexN_1 indexN_2 indexN_3 ...;  %# Index N for all time stamps
 value1_1 value1_2 value1_3 ...;  %# Table value 1 for all time stamps
 value2_1 value2_2 value2_3 ...;  %# Table value 2 for all time stamps
 ...
 valueN_1 valueN_2 valueN_3 ...]  %# Table value N for all time stamps

For each column, there is a time stamp, N elements for the lookup table indices, and N elements for the table values. Once loaded using the From File block, the output of length 2*N can be split into two outputs each of length N (i.e. the indices and the table values) using the Demux block. These two arrays can then be used for the xdat and ydat inputs to the Lookup Table Dynamic block, thus creating a lookup table whose index and table values can be updated from a file as the simulation runs.

Response to comment from AKE...

The time stamps are present in the above array because I was under the impression that you wanted to change the lookup table data as a function of simulation time (e.g. use one set of indices and table values for 0 to 10 seconds, then a different set for 10 to 20 seconds). If you want to do this, it will require some specification of the times at which the data will be changed.

However, if you only want to load one set of table data from a file at the start of the simulation, then your .mat file should only need one column with a time stamp of 0. Your sample data in A can be easily modified accordingly:

A = load(yourDataFile);  %# Load your data 
A = [0; A(:)];           %# Convert it to a column vector and add a time stamp
save(yourMatFile,'A');   %# Save A to a .mat file for the From File block

With regard to your concern about the Demux block, you actually shouldn't need to specify N. You only need to specify that it will have 2 outputs, and it will thus divide the input in half. For example, if the input is a 10-element vector, and you have two outputs specified for the block, you will get two 5-element vectors as the output.

Share:
13,670
Assad Ebrahim
Author by

Assad Ebrahim

Updated on June 08, 2022

Comments

  • Assad Ebrahim
    Assad Ebrahim almost 2 years

    I've built a Matlab/Simulink model that I'm using to simulate the performance of an underwater robotic vehicle that uses acoustics for various key navigation and localisation functions.

    Since the characteristics of the ocean change with seasonality and geolocation, I would like this data to be dynamically loaded into the model from an ASCII data file (space separated data organized in rows and columns).

    Simulink has a number of Lookup Table Blocksets, but none of them seem to provide a "read from file" option directly. Having to use the Table Editor would be taking the model in the wrong direction.

    Is there another way, perhaps using Matlab, to load data into the Blockset from a file?

    For a 1-D table, I'm looking for something akin to the Matlab commands

    A = load(filename)
    A(:,1)  % for the index
    A(:,2)  % for the table values
    

    AKE

  • Assad Ebrahim
    Assad Ebrahim over 13 years
    Thanks gnovice. Yes, this is direction, but I think there are two problems here. The first problem is that From File block seems to require that .mat data be in *time series form -- there's no notion of time in data tables I'm using, so this would be an artificial addition that would require an additional "glue script". A second problem I believe is that the Demux block needs to know a static N value, and can't discover it (correct?) The goal is to be able to have N be dynamic -- discovered at the runtime of the simulation.
  • Assad Ebrahim
    Assad Ebrahim over 13 years
    Thanks MikeT -- this works for a static table, but not for a dynamic table. I'd like to avoid using startup scripts or workspace variables, since this means having to keep the data up-to-date outside the simulation. The ideal solution would be somehow being able to load the table data from a file during the runtime of the simulation. This would allow, for example, environmental parameters (table data) to evolve slowly in time as the simulation runs.
  • Assad Ebrahim
    Assad Ebrahim over 13 years
    nice and simple! both concerns addressed -- this works well. In fact, I think the timing index should also solve the time-based dynamic update issue. (I had in mind another approach so missed your implication until your elaboration above.) Thanks ;)
  • Assad Ebrahim
    Assad Ebrahim over 13 years
    +1 for simplicity and +1 for coming back to clarify -- which brought out the value.
  • MikeT
    MikeT over 13 years
    I misunderstood your question, I completely agree with gnovice's advice, that's the right way to do it.