MATLAB Answers


loading .mat files in a simulink user defined function

Asked by John
on 23 Jul 2014
Latest activity Commented on by John
on 22 Aug 2014

Hello! I am working on some model validation and have built a 'Matlab function' block in Simulink. In the Matlab function I need to load some .mat files. The .mat files take and input value and return an output value that is used within the main Matlab function. The 'Matlab function' block is basically a copy of a .m file I have created and used before, (it works in matlab). Simulink has issues loading the .mat files however and I am not sure how to get around this problem. I assume this is because the .mat files are 'defined' in Matlab and not available to Simulink, though I am not sure why. Any ideas? Tips?



No products are associated with this question.

1 Answer

Answer by Azzi Abdelmalek
on 23 Jul 2014

You didn't post how you are downloading your file, I guess you have used the command

load filename

If your mat file contains, for example three variables x,y and z, add this code



on 24 Jul 2014

I have used the command

load 'filename.mat'

which can take an input and return another number, the file 'filename' is a spline interpolant [1x1 cfit] created/exported from the basic fitting GUI, (basically a lookup table). As mentioned, the code works fine in Matlab, because the file 'filename' is available. This doesn't seem to be the case in Simulink...

Thanks again for your replies!

Please, shows us how you are trying to load your data, and why you can't use

on 22 Aug 2014

the file 'your_mat_file' as in your example above is a [1 x 1 cfit] spline so when I use the commands

filename = 'my_mat_file' load 'filename'

Then I can use 'filename' to get values from the file as a function of inputs as follows

test = filename(5,17) test = 35.8


test = filename(1,234) test = 983.2

for example.

Simulink doesnt seem to understand that this mat. file is a fitted spline- like funciton.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!