MATLAB Answers

How to change variable in base workspace without blocking the thread?

4 views (last 30 days)
Thijs Nulle
Thijs Nulle on 20 Oct 2020
Commented: Thijs Nulle on 21 Oct 2020
I am trying to link an arduino to my simulink model and let the arduino send messages to change variables in my simulink model. The receiving end goes well with the serial receive block however I do not know how to change the variable in the base workspace without blocking the thread. If I use assignin or evalin it shows with tic; toc; that it takes around 0.0003 seconds, however the screen freezes for around 0.2 seconds when the method is called. So this method is not an option.
Then I tried using batch to run those methods it in the background or parfeval to run it in parallel, but they are both making the program even slower.
So my question, is there a way to target variables in another workspace without blocking the simulink thread? Or is there a way to queue the change of the variables since I don't need it immediately until a moment where not a lot is happening?

  2 Comments

Ameer Hamza
Ameer Hamza on 20 Oct 2020
The real question is, why are you trying to change variables in the base workspace continually?
Thijs Nulle
Thijs Nulle on 20 Oct 2020
I am not trying to change variables continuously, I want to be able to change a variable in the base workspace (which is the workspace the simulink model is using) without interfering with the execution of the model. And according to the time MatLab says assignin takes, I should not be able to tell any hiccup but it does... So I would like to know how I could change it in the background.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 20 Oct 2020
Are you using the Simulink Support Package for Arduino Hardware? I have not used this myself but I would be surprised if it didn't allow you to pass data from Simulink to the Arduino hardware and vice versa. If you're not aware of that support package, open the Add-Ons Explorer (in the Environment section of the Home tab on the Toolstrip) and search for "Arduino".

  3 Comments

Thijs Nulle
Thijs Nulle on 20 Oct 2020
I have got the communication working, that is not the problem. However, I want to parse the data being sent and assign the value of a variable in the base workspace to the data being received. But if I use assignin my simulink model halts for around 0.1-0.2 seconds, and I would like to know if it possible to either run assignin in a different thread to not interfere with the main calculation or if I can maybe queue the execution of the method to only happen when the resources are available.
Steven Lord
Steven Lord on 20 Oct 2020
I'm not certain, but if you use the tools provided by the support package you may not need to involve the base workspace at all. That could avoid the 0.1-0.2 second "halts" you described entirely.
Thijs Nulle
Thijs Nulle on 21 Oct 2020
If all the variables for my simulink model are stored in the base workspace I definitely have to access it at some stage. I have currently tried running a C-script with mexPutVariable, however this still gives the same error. I also tried using C code to use pointers to the variables however MatLab just doesn't like pointers. Is there not any way to change variables in the base workspace without using assignin? Is there a way to auomate the command window programmatically?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by