views:

64

answers:

2

Hello,

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

A: 

The table data can be a variable in the MATLAB workspace, and you can populate the workspace by loading any file you want before running the simulation. So in your example, the variable A can be used as the table data.

Write a startup script to populate your workspace before running the simulation, in that script you can load the data from any mat file you'd like.

MikeT
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.
AKE
I misunderstood your question, I completely agree with gnovice's advice, that's the right way to do it.
MikeT
+1  A: 

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.

gnovice
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.
AKE
@AKE: I updated my answer to address your above concerns.
gnovice
@gnovice: 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 ;)
AKE
+1 for simplicity and +1 for coming back to clarify -- which brought out the value.
AKE
@AKE: Glad to help. ;)
gnovice

related questions