Load data/.mat-file in Simulink

  • Follow


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
0
Reply Calum 10/26/2010 6:00:08 PM

"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
0
Reply Arkadiy 10/27/2010 3:28:05 PM


"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
0
Reply Calum 11/2/2010 8:48:04 AM

2 Replies
1072 Views

(page loaded in 0.042 seconds)

Similiar Articles:













7/20/2012 8:48:23 PM


Reply: