MATLAB Answers

passing GUI workspace variables to simulink model

12 views (last 30 days)
Philip
Philip on 29 Dec 2013
Commented: Azzi Abdelmalek on 29 Dec 2013
I'm having an issue getting my GUI and Simulink model to place nice together.
My GUI has a great many fields that the user can customize with input, values for k0, k1, k2, b0, b1 etc.
Upon pressing a button, my GUI should call a simulink model that I have already made to evaluate the system.
My problem is that when I run the sim('modelname') command (with modelname replaced by my own model's name), the variables I have in the workspace aren't passed along, so MATLAB tells me "error evaluating parameter..." and "undefined function or variable 'k0' for all the variables I had just imported from my GUI.
I had originally looked around and tried to pass them to simulink, creating a parameter variable that has parameter.k0 = k0; etc for all the values and then running sim('modelname', parameter); however, this gives me the error block_diagram does not have a parameter named "L".
How do I run my Simulink model from my GUI using the variables it needs in the correct workspace?
kind regards,

  0 Comments

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 29 Dec 2013

  3 Comments

Philip
Philip on 29 Dec 2013
I had looked at this, but since I have 17 different parameters, would I not need to set them all separately, creating an unwieldy line; set_param('modelname',k0,k0,k1,k1,... etc?
I will try implementing this and let you know if I have any problems, but is there no more elegant solution?
I had previously run the simulation using an m-file (ie no GUI, to test the simulation) and here I could just declare all my variables in the m-file and then sim('modelname') didn't have any problem. Is this because then everything is done in the base workspace?
Philip
Philip on 29 Dec 2013
set_param gives me a similar error, telling me "Block-diagram does not have a parameter 'k0'".
Does this mean I need to set every instance where a variable is used (each gain, each subfunction) separately with its own set_param line? This would totally negate the advantage of declaring variables for the varying blocks.
Is there no way to share variables between workspaces short of making them global?
Azzi Abdelmalek
Azzi Abdelmalek on 29 Dec 2013
Try another option: save your parameters in a mat file, and load it in the callback of your simulink model

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!