Using an S-function to access a mat-file and load data to array
3 views (last 30 days)
I am working with a simulink model in MATLAB 2013a 64-bit. I want to create a function that takes in two inputs (azimuth and elevation), and accesses a mat-file on the matlab path that contains data related to these coordinates. For example, if the block takes in az = 30 and el = -25, the routine should access a file called "az30_el-25_data.mat", and load the contents to an array which is them output from the block to be used in the simulation. The routine would run every time the az or el changes by a degree (rounded to nearest integer), so not at every timestep. I was unsuccessful in creating an embedded matlab function that could accomplish this (I cannot use extrinsic as I want to generate code), because the load function can only be used with a string that is defined prior to compile time (I could not load a filename stored in a variable). I tried creating the file name in one function and passing that filename to another EML function as doubles and loading it in another function, but I was unsuccessful with this as well for the same reason. My next step is going to be trying to accomplish this task with an S-function. I have never written an s-function before, so I wanted to know (before I exert too much effort with this) if it will be possible with an s-function, and if so which type of s-function it would be easiest to accomplish this with. I would prefer to use a MATLAB s-function, with a c s-function only if completely necessary.
Any advice surrounding solving this issue is much appreciated.
Here is some code demostrating what I am attempting to accomplish. This code is incomplete and will not run, but gives an idea of what I want to do.
function [filename] = import_sc_data(az, el) %#codegen
% Create azimuth string label
if az == 0
az_str = 'az0';
elseif az < 0
az = -az;
az_str = ['az-' convertnum(az)];
az_str = ['az' convertnum(az)];
% Create elevation string label
if el == 0
el_str = 'el0';
elseif el < 0
el = -el;
el_str = ['el-' convertnum(el)];
el_str = ['el' convertnum(el)];
% Concatenate strings to create file name to access
filename = ['sc_data_' az_str '_' el_str '.mat'];
% Load stored scattering center data
stored_data = load(filename);
%Initialize output array
ScatteringCenters = zeros(500,14);
% Populate array with data from mat-file
Ryan Livingston on 14 May 2015
I assume that the values to be stored in the hypothetical MAT files would not change after generating code, is that correct?
If so, and if you would like to use a MATLAB Function Block, my recommendation would be to set up a persistent variable that stores all of the required parameters and then read the values from that persistent at runtime. This could be accomplished by loading a single MAT file while generating code.
Suppose you save the parameters in the variable params in the file params.mat then a quick example would be:
function y = foo(az,el)
data = coder.load('params.mat');
params = data.params;
% Get data from parameters
idx = convert_az_el_to_index(az,el);
settings = params(idx);
% Use settings
The part that may take a little ingenuity is how to map az and el to an index into the params array that contains all of the data.
Using this approach, there is no need to do file I/O on several time steps of the model. One downside is that if you change the data in the MAT files, you'll need to generate code again.