MATLAB: Convert matlab variables to simulink parameters

convert matlab variables to simulink parameters

I have 700 matlab variables in my workspace. I want to convert all variables to simulink parameters. Do you know any easy way to convert the data? In my situation, I add parameters to workspace for convert all variables at once. it's really hard….

Best Answer

Write a script to collect the name, data type etc of all your workspace variable and make a loop to create Simulink.Parameter variables based on above info.
% Clear workspace
clear variables;
% Load variabled which are to be onverted in base workspace
% load here from m-file for mat-file or just create
param1 = 100;
param2 = 100;
% List of all workspace variables
wsVarList = whos;
for x=1:length(wsVarList)
% Collect variable name
varName = wsVarList(x).name;
% Save value. This variable will be replaced by Simulink.Parameter
varVal = eval(varName);
% Create parameter with same name as variable name and assign value
eval([varName ' = Simulink.Parameter']);
eval([varName '.Value = ' num2str(varVal)]);
% Based on your requirement, you can assign other properties
% wsVarList(x).class
% wsVarList(x).size
end