MATLAB Answers


passing structure from matlab workspace to embedded matlab function block

Asked by hasan
on 16 May 2012


How can i pass a structure that is defined in matlab workspace to simulink embedded matlab function block. e.g :



These are defined in matlab ws. Then by using the input port in simulink, can i pass this to matlab emb function block in simulink, or any other way?

Thanks four your replies...

  1 Comment

Kyle: I would recommend posting your answer under the Answers section instead of as a comment on the question. Thanks! :)

Log in to comment.

2 Answers

Answer by Kaustubha Govind on 16 May 2012

You will need to define a Simulink.Bus signal to import the structure into Simulink as a nonvirtual bus - see Importing Structures of MATLAB timeseries Objects for Bus Signals to a Root-Level Input Port. Also refer to Working with Structures and Bus Signals in the MATLAB Function block documentation.


OK i did create a Simulink.Bus which has two elements a and b. But how can i assign values to a and b from Matab ws. Simply writing Number.a equal something gives an error. "no public field a exists for class Simulink.Bus". Any idea?

The Simulink.Bus object is used to specify the datatype of the signal (you can configure this as the output type of the Inport block and the input type of the MATLAB Function blok). To actually import your data via the Inport block, please refer to the first link in my answer. You will need to create a structure of timeseries objects.

Log in to comment.

Answer by Kyle Andringa on 28 Nov 2012

Yes, this can be done. As you mentioned, go into the EditData (Ports and Data Manager) page of the embedded Matlab file. Define a variable and choose Input as the Scope. Then on the outside of the embedded Matlab file assign the input to a Constant block. Within the Constant block, assign the structure (Numbers). On the signal attributes of the constant block, you must assign the output to a Bus signal which matches the structure (Numbers). This process is compatible with Rapid Accelerator.

Alternatively, you could choose the Scope as Parameter instead of Input. In this case you just name the Parameter "Numbers". This method is not compatible with Rapid Accelerator.

In both cases, you can only go one layer deep within the structure at a time. However, you can dig deeper in multiple steps. Example:

It doesn't like: new_var = Numbers.A.a But you could do: A = Numbers.A; a = A.a;

I'm not sure the reasoning for all of these nuances, I just know these items from (painful) experience.

Good Luck.


Log in to comment.

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!