In the development of Simulink models, achieving data sharing across workspaces is key to enhancing modular design and system maintainability. By controlling with MATLAB scripts, variables in the base workspace, model workspace, and data dictionary can be efficiently managed to ensure data consistency and accessibility. This article will delve into the technical implementation and best practices.
📁 1. Types of Workspaces and Scope Rules
MATLAB provides three core workspaces:
1. Base Workspace
– Globally visible, variable lifecycle lasts until the session ends.
– Suitable for global parameters or temporary data.
2. Model Workspace
– Bound to a specific Simulink model, supports shared, private, and nested scopes.
– Takes precedence over the base workspace for variable resolution, avoiding naming conflicts.
3. Data Dictionary
– Supports persistent storage, version control, and team collaboration.
– Suitable for managing large sets of parameters and system configurations.
Scope rules: The model prioritizes resolving variables from its own workspace, and if not found, it retrieves from the base workspace.
💾 2. Data Storage Strategies and Choices
Storage Location | Applicable Scenario | Typical Operation
Model Workspace | Parameters used by a single model | Simulink.data.assignin(model, ‘Kp’, 1.5)
Base Workspace | Temporary data shared across models | assignin(‘base’, ‘u’, sin(t))
Data Dictionary | Large sets of parameters and system configurations | dict = Simulink.data.dictionary.open(‘myDictionary.sldd’);
Strategy Recommendations:
– Single model parameters → Model Workspace
– Cross-model shared data → Base Workspace or Data Dictionary
🔄 3. Data Import and Export Methods
1. Import Data (From Workspace)
matlab
t = 0:0.1:10;
u = sin(t); % Preloaded into the base workspace
Simulink Configuration:
– Use the From Workspace block to directly read u and t.
2. Export Data (To Workspace)
matlab
set_param(‘myModel/To Workspace’, ‘VariableName’, ‘outputData’);
set_param(‘myModel/To Workspace’, ‘SaveFormat’, ‘StructureWithTime’);
Post-export Operation:
matlab
simOut = sim(‘myModel’);
outputData = simOut.get(‘outputData’);
⚙️ 4. Advanced Data Interaction Techniques
1. MATLAB Function Block
matlab
function y = processData(u)
persistent dataCache;
if isempty(dataCache), dataCache = []; end
dataCache = [dataCache; u]; % Accumulate data
y = u;
end
2. Model Callback Functions
matlab
function stopCallback(model)
save(‘simResults.mat’, ‘outputData’); % Automatically save at the end of simulation
end
🔗 5. Cross-module Access to Shared Object Instances
1. Handle Class Definition
matlab
classdef SharedObject < handle
properties
Value = 0;
end
methods
function update(obj, increment)
obj.Value = obj.Value + increment;
end
end
end
2. Data Dictionary Storage
matlab
sharedObj = SharedObject;
dict = Simulink.data.dictionary.open(‘myDictionary.sldd’);
addData(dict, ‘sharedObj’, sharedObj);
🚀 6. Script Control and Automation
Parameter Scanning Example
matlab
for Kp = [0.5, 1.0, 1.5]
simInput = Simulink.SimulationInput(‘myModel’);
simInput = setVariable(simInput, ‘Kp’, Kp);
simOut = sim(simInput);
results{Kp} = simOut.get(‘outputData’);
end
🌟 7. Best Practices and Common Issues
Best Practices
1. Variable Naming: Use prefixes to distinguish sources (e.g., ModelA_Kp).
2. Lifecycle Management: Explicitly initialize variables and clean up temporary data after simulation.
3. Error Handling: Use try-catch to capture variable resolution errors.
4. Performance Optimization: Reduce cross-workspace operations, prioritize using data dictionaries.
Common Issues
– Variable resolution failure: Check variable location with which -all varName.
– Performance degradation: Avoid frequent read/write operations in simulation loops.
🔍 Summary
Implementing data sharing across multiple workspaces in Simulink requires a comprehensive application of workspace management, data storage strategies, and script automation techniques:
1. Scope Rules: Understand the priority of different workspaces;
2. Storage Choices: Select between model workspace, base workspace, or data dictionary based on the scenario;
3. Advanced Techniques: Utilize MATLAB Function blocks and data dictionaries for complex interactions.