Using an S-function to access a mat-file and load data to array

4 vues (au cours des 30 derniers jours)
Collin
Collin le 6 Mai 2015
Commenté : Marcus Jansson le 20 Juin 2017
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

Réponses (2)

Ryan Livingston
Ryan Livingston le 14 Mai 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 commentaires
Collin
Collin le 14 Mai 2015
Thanks for the response.
The content of the mat-files does not change, however which mat-file i want to access during the run will change. The az and el values determine which mat-file will be accessed, they are not an index in the mat-file. For example, given an azimuth of 30 deg and an elevation of 40 deg, i might need to access a mat file called az40el30.mat. There are far too many data points to put them into one and access (500x14x4 for all possible az-el degree combinations for many possible targets). It seems to me that the load function only works in a matlab function given that the string is a constant, which would not be the case for me. The function would need to determine what the file is called, and load the corresponding mat-file.
The only way that this seems possible at this point is through a C S-function (reading in binary files, as mat-file api is not supported for code generation), which I am attempting to tackling right now. Thanks again for getting back to me, please let me know if you see another solution.
Collin
Ryan Livingston
Ryan Livingston le 14 Mai 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.

Connectez-vous pour commenter.


ali moshkriz
ali moshkriz le 2 Juil 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...

Community Treasure Hunt

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

Start Hunting!

Translated by