Clear Filters
Clear Filters

How can i stop my data been imported from simulink to matlab workspace to be in cell array?

3 views (last 30 days)
after my simulation this is how the data from simulink to matlab workspace look like. i want all data imported to matlab workspace but not in cell array

Answers (2)

Hassaan
Hassaan on 15 Jan 2024
Edited: Hassaan on 15 Jan 2024
When data from a Simulink simulation is imported into the MATLAB workspace, it often comes in the form of a Simulink.SimulationData.Dataset or a cell array, especially if you're logging signals with the "To Workspace" block or using signal logging.
Here are a few approaches:
Convert to a Matrix or Array: If the data in each cell is numeric and of the same size and type, you can concatenate them into a matrix:
% Concatenate all numeric data from the cell array into a matrix
matrixData = cat(1, simData{:});
Convert to a Table: If the data represents different variables, a table might be a good way to store the data:
% Convert the cell array to a table
tableData = cell2table(simData);
Convert to a Structured Array: If the data in each cell is a struct with the same fields, you can convert the cell array of structs to a structured array:
% Assuming each cell contains a struct with the same fields
structuredArray = [simData{:}];
Convert to a Time Series Object: If the data is time-series data, you might want to convert it to a timeseries object or an array of timeseries objects.
% Create an array of timeseries objects if each cell contains one timeseries
timeSeriesArray = cell2mat(simData);
Extract Specific Fields: If each cell contains a complex object (like a Simulink.SimulationData.Dataset), and you're only interested in specific fields, you could extract those:
% For example, if you only want the 'signals' field from a Dataset
extractedData = cellfun(@(c) c.signals, simData, 'UniformOutput', false);
% Now extractedData is a cell array of 'signals' fields
% You can further process this to extract exactly what you need
Deal with Varying Sizes: If the sizes of the data in each cell vary, you might need to deal with them on a case-by-case basis or use cell arrays of varying sizes.
To convert the data from a cell array to individual variables in the MATLAB workspace, you can iterate over the cell array and assign each cell's contents to a unique variable. Here's an example of how to do this:
% Assuming 'simData' is your cell array from Simulink
for k = 1:length(simData)
% Create a variable name based on the cell index
varName = sprintf('simData%d', k);
% Assign the cell's contents to a variable in the base workspace
assignin('base', varName, simData{k});
end
This script will create variables named simData1, simData2, and so on, each containing the data from the corresponding cell of simData.
If the cell array contains timeseries or Simulink.SimulationData.Dataset objects, and you want to extract just the numerical data, you would need to handle each case appropriately. For instance, if simData{k} is a timeseries object:
for k = 1:length(simData)
if isa(simData{k}, 'timeseries')
% Extract the data and time from the timeseries object
data = simData{k}.Data;
time = simData{k}.Time;
% Create variable names
dataVarName = sprintf('simData%d_Data', k);
timeVarName = sprintf('simData%d_Time', k);
% Assign to variables in the base workspace
assignin('base', dataVarName, data);
assignin('base', timeVarName, time);
elseif isa(simData{k}, 'Simulink.SimulationData.Dataset')
% Handle Dataset case
% ...
else
% Handle other cases or throw an error
error('Unsupported data type in cell array.');
end
end
This will create pairs of variables for each timeseries object: one for the data and one for the time values.
Note that using assignin is generally discouraged because it can lead to code that is hard to debug and maintain. It's usually better to work with structured data types like structures, tables, or classes that encapsulate the related data. However, if you really need separate variables in the workspace, assignin is one way to accomplish that.
---------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
richard uwagbale
richard uwagbale on 15 Jan 2024
Thank you for yor response, but i want to export all the variables stored in the cell array without assigning it to a new variable. for example if i open the cell array data been exported to matlab workspace, i see data like 'simout_Egy_pnt_kwh'. so i want it exported to matlab workspace without assigning to a new variable,because i have hundreds of data saved in the cell array been exported from simulink. if this is not possible is there a way i could stop my simulink from exporting data in that manner?

Sign in to comment.


Hassaan
Hassaan on 15 Jan 2024
Edited: Hassaan on 15 Jan 2024
@richard uwagbale See this and my previous updated answer.
If your Simulink model is exporting simulation data as a cell array and you want to have all the variables from that cell array available directly in the MATLAB workspace, there are a couple of ways you can handle this.
Firstly, if you want to prevent Simulink from creating a cell array and instead write each variable directly into the workspace, you should adjust the configuration parameters for your Simulink model. Here’s how you can do it:
  1. Open the Model Configuration Parameters dialog by selecting Simulation > Model Configuration Parameters from the menu in your Simulink model.
  2. Go to the Data Import/Export section.
  3. You can configure the logging of states, outputs, and other signals. For example, for signal logging, uncheck the "Log signals as single object" option to log each signal as a separate variable in the workspace.
  4. You may also use the "To Workspace" blocks in your model to export data to the workspace. Ensure the "Save format" is set to "Array" or "Structure With Time", not "Cell Array".
---------------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  2 Comments
Hassaan
Hassaan on 15 Jan 2024
Thats good. You are welcome.
-------------------------------------------------------------------------------------------------------------------------------------------------------- If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback. Professional Interests Technical Services and Consulting Embedded Systems | Firmware Developement | Simulations Electrical and Electronics Engineering Feel free to contact me.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!