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.
Thanks,
Collin
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)];
else
az_str = ['az' convertnum(az)];
end
% Create elevation string label
if el == 0
el_str = 'el0';
elseif el < 0
el = -el;
el_str = ['el-' convertnum(el)];
else
el_str = ['el' convertnum(el)];
end
% 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
...
end

Answers (2)

Ryan Livingston
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)
persistent params;
if isempty(params)
data = coder.load('params.mat');
params = data.params;
end
% 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.
  2 Comments
Ryan Livingston
Ryan Livingston on 14 May 2015
Makes sense, thanks for the clarification, that would indeed be a large array to read all at once.
The only other option I see from the MATLAB Function Block is to store the data in binary or text files rather than MAT files and read the data using coder.ceval and either the C fscanf or fread. There is an example showing how to do this using fread but fscanf would work in a similar way. This approach would not need a constant file name as you would be opening the file using the C fopen.
The choice clearly depends on what seems simpler and more flexible to you given your needs.

Sign in to comment.


ali moshkriz
ali moshkriz on 2 Jul 2016
Hery guys i wanna to use a data in s-function . how i can i do it ? my data is related in two variable as x,y i upload a photo af theme here...

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!