Load Variable for Use in Simulink MATLAB Function Block

I have been working on this for a while, but cannot get it to work. Basically, I have a MATLAB function block used in a Simulink model. In this block, I have something along the lines of
persistent a
if isempty(a)
load a % a is MAT file with 'a' defined in it
end
%use variable a
But that gave an error. So I tried this
persistent a
if isempty(a)
a = load('a.mat') %a is a structure now
end
%use a.a now
That gave the error: Attempt to extract field 'a' from 'mxArray'.
I'm not sure what to do.

2 comentarios

TAB
TAB el 19 de Abr. de 2012
How you are using structure "a" further?
This error will appear probably if you will assign a.a (which is a matlab variable) directly to output.
Ali
Ali el 19 de Abr. de 2012
I use a in numerous MATLAB functions.
sum
mean
bsxfun (I say a = bsxfun(@minus,a,b);)
numel
I also do .^2 on it too.
And in these many functions, i often index into a
i.e. (:,2) or (:,1), etc.

Iniciar sesión para comentar.

Respuestas (1)

Kaustubha Govind
Kaustubha Govind el 19 de Abr. de 2012
It looks like you are using 'a' like a parameter. Why not declare it as one, so that the value is automatically picked up from the workspace? See Parameter Arguments in MATLAB Function Block Functions. Just remember to not "write" to a. Treat it as read-only.

Categorías

Más información sobre Simulink Functions en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Ali
el 19 de Abr. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by