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

Thread Subject:
Load data/.mat-file in Simulink

Subject: Load data/.mat-file in Simulink

From: Calum

Date: 26 Oct, 2010 18:00:08

Message: 1 of 3

Hello all,

I have a Matlab function which imports and uses data from a .mat file. This is held in an array of structures, with each structure holding several elements.
I am trying to convert this function into an embedded Matlab block in a Simulink model.
This block needs to access the data in the .mat file. Ideally, it would load all the data in the .mat file at t=0 and this would persist in memory throughout the simulation.
I've tried to do this in various ways:

- Loading the .mat file directly from within an EML block doesn't work as load() is an extrinsic function, and Matlab is not able to index into/make use of the resulting mxArray.
- Using a constant block in Simulink to import the data doesn't work. It appears to be possible to feed each element of the struct into a separate block and input that to the EML function, but this would mean that only one value per timestep is loaded.
- Using a 'from workspace' block in Simulink doesn't work. This appears to only apply to a vector of values, each of which has a timestep associated with them. This doesn't fit the format of the data I'm using.
- Using a data store and memory block doesn't work as it appears to only load one element of data for each timestep.

None of these options appear to work - does anyone have any suggestions?

thanks
C

Subject: Load data/.mat-file in Simulink

From: Arkadiy Turevskiy

Date: 27 Oct, 2010 15:28:05

Message: 2 of 3

"Calum " <cgb7@nonsense.hw.ac.uk> wrote in message <ia74v8$qnn$1@fred.mathworks.com>...
> Hello all,
>
> I have a Matlab function which imports and uses data from a .mat file. This is held in an array of structures, with each structure holding several elements.
> I am trying to convert this function into an embedded Matlab block in a Simulink model.
> This block needs to access the data in the .mat file. Ideally, it would load all the data in the .mat file at t=0 and this would persist in memory throughout the simulation.
> I've tried to do this in various ways:
>
> - Loading the .mat file directly from within an EML block doesn't work as load() is an extrinsic function, and Matlab is not able to index into/make use of the resulting mxArray.
> - Using a constant block in Simulink to import the data doesn't work. It appears to be possible to feed each element of the struct into a separate block and input that to the EML function, but this would mean that only one value per timestep is loaded.
> - Using a 'from workspace' block in Simulink doesn't work. This appears to only apply to a vector of values, each of which has a timestep associated with them. This doesn't fit the format of the data I'm using.
> - Using a data store and memory block doesn't work as it appears to only load one element of data for each timestep.
>
> None of these options appear to work - does anyone have any suggestions?
>
> thanks
> C

You can do this using a callback:
1) Open your Simulink model
2) Click "File" -> "Model Properties"
-3) Select the second tab, called "Callbacks" in the "Model Properties" window that opened up
4) Select the right callback, and type in the name of your MATLAB function.
You probably can use "PreLoadFcn" callback. The in the "Model pre-load function:" part of the window, type your function, i.e., type loaddata if your function is loaddata.m.

Here is the doc reference:
http://www.mathworks.com/help/toolbox/simulink/ug/f4-122589.html

HTH.
Arkadiy

Subject: Load data/.mat-file in Simulink

From: Calum

Date: 2 Nov, 2010 08:48:04

Message: 3 of 3

"Arkadiy Turevskiy" <arkadiy.turevskiy@mathworks.com> wrote in message <ia9ge5$dli$1@fred.mathworks.com>...
> "Calum " <cgb7@nonsense.hw.ac.uk> wrote in message <ia74v8$qnn$1@fred.mathworks.com>...
> > Hello all,
> >
> > I have a Matlab function which imports and uses data from a .mat file. This is held in an array of structures, with each structure holding several elements.
> > I am trying to convert this function into an embedded Matlab block in a Simulink model.
> > This block needs to access the data in the .mat file. Ideally, it would load all the data in the .mat file at t=0 and this would persist in memory throughout the simulation.
> > I've tried to do this in various ways:
> >
> > - Loading the .mat file directly from within an EML block doesn't work as load() is an extrinsic function, and Matlab is not able to index into/make use of the resulting mxArray.
> > - Using a constant block in Simulink to import the data doesn't work. It appears to be possible to feed each element of the struct into a separate block and input that to the EML function, but this would mean that only one value per timestep is loaded.
> > - Using a 'from workspace' block in Simulink doesn't work. This appears to only apply to a vector of values, each of which has a timestep associated with them. This doesn't fit the format of the data I'm using.
> > - Using a data store and memory block doesn't work as it appears to only load one element of data for each timestep.
> >
> > None of these options appear to work - does anyone have any suggestions?
> >
> > thanks
> > C
>
> You can do this using a callback:
> 1) Open your Simulink model
> 2) Click "File" -> "Model Properties"
> -3) Select the second tab, called "Callbacks" in the "Model Properties" window that opened up
> 4) Select the right callback, and type in the name of your MATLAB function.
> You probably can use "PreLoadFcn" callback. The in the "Model pre-load function:" part of the window, type your function, i.e., type loaddata if your function is loaddata.m.
>
> Here is the doc reference:
> http://www.mathworks.com/help/toolbox/simulink/ug/f4-122589.html
>
> HTH.
> Arkadiy


Thanks, that seems to do the trick.
C

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us