Effacer les filtres
Effacer les filtres

Simulink - Saving Base Workspace Variables

13 vues (au cours des 30 derniers jours)
James Vrtis
James Vrtis le 6 Oct 2023
Commenté : Paul le 7 Oct 2023
Is there a way to save the Simulink "base workspace" variables from the .m file? Everytime I open my model in Simulink, I need to re-inport the base workspace, but would rather have the variables saved in the model.

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 6 Oct 2023
Modifié(e) : Fangjun Jiang le 6 Oct 2023
There are at least two other ways.
  1. Once all the .m files are executed, you can use save() to save the base workspace to a .mat file. Next time, you use load() to load it. This might be faster if .m files have lots of "computing" involved. On the onter hand, .m files are text and easily visible. So the choice is case dependent.
  2. You can transfer your variables from .m file or base workspace to a Simulink Data Dictionary (SLDD, .sld) file and then "attach" it to the model. Search document for how to use it.
In any case, there is no direct way to save all the variables directly in the Simulink model file (.mdl or .slx). And it is not a good idea to do that.
A stretch way is to copy and past the .m text to the "PreLoadFcn" of the Simulink model so you could end up with one Simulink model file that includes both the model contents and base workspace variables.
Or if you are looking for convenience, then you can type all those .m file names in the "PreLoadFcn" of the model. All you need is to open the Simulink model and all the .m files will be automatically executed.
  1 commentaire
James Vrtis
James Vrtis le 6 Oct 2023
Thank you for the help!

Connectez-vous pour commenter.

Plus de réponses (1)

Paul
Paul le 6 Oct 2023
One option is to use the Model Workspace: "Each model is provided with its own workspace for storing variable values."
  2 commentaires
Fangjun Jiang
Fangjun Jiang le 7 Oct 2023
That is true. In that sense, the variables are saved directly with the model in a single file.
Paul
Paul le 7 Oct 2023
There is also an option to have the variables stored in a .mat file:
  • When the model is loaded, the workspace is initialized from a data source.The data source can be a Model file, a MAT-file, a MATLAB file, or MATLAB code stored in the model file. For more information, see Data source.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by