Stuart’s MATLAB Videos

Watch and Learn

Writing out your workspace variables as a script with Simulink.saveVars

I almost never use Simulink, so I never saw this function before (requires Simulink). Try this at the command line:
a = 1;
b = 2.5;
c = 'A string';
d = {a, b, c};

Simulink.saveVars('MyVars');
It produces a file like this:
% -------------------------------------------------------------------
%  MATLAB file generated by Simulink.saveVars on 27-Sep-2010 13:52:16
%  MATLAB version: 7.11.0.584 (R2010b)
% -------------------------------------------------------------------

a = 1;

b = 2.5;

c = 'A string';

d = cell(1, 3);
d{1} = 1;
d{2} = 2.5;
d{3} = 'A string';

I can see something like this being a lot more transparent than saving variables to a .mat file. for small variables. For example, if you want to take your current workspace and use it as the basis for a script. This script can be modified easily, and you can even call saveVars again to write new or updated data into your modified script.

It’s quite configurable.  For instance, you can specify the size threshold above which large variables will just be saved to a .MAT file instead of written to the script (the script automatically loads the .MAT file). You can even control the maximum width of the generated code.

You might find that it will gives me a more readable script than simply loading a .mat file. People reading your code will see more of what you are doing. This helps with readability.

What kinds of uses can you think of for this cool function?

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.