How to display results obtained from simulink

5 views (last 30 days)
nkumar
nkumar on 27 Jun 2015
Answered: Purvaja on 3 Mar 2025
I have a Simulink model in which I have added 15 to workspace blocks(array format).I want to write these values with variables into excel sheet.I have done manually buy tying all variable names and ten use xlswrite,but it takes time.is there any way to do without manualy entering.I tried simOutVar = simOut.who,it displays variables.but I need with values to display . simOut.get()also I tried but have to enter each time the variable name
for ex I used in to workspace block names as result1 to result 15
I need to display like(with values were I run for 10sec,i dispaled 2 answers)
result1 result2.......result15
0.23 0.98 0.84
0.20 0.99 0.99
is it possible

Answers (1)

Purvaja
Purvaja on 3 Mar 2025
Hi @nkumar,
I understand that you wish to store the outputs of “To Workspace” block in "array" format into an excel sheet with output columns identified by their respective block names as stored in Simulink model.
I have assumed some dummy signals using pulse generator for some duration and stored them in six “To Workspace” blocks. I have used some logical operator to bring variations in signal stored. I have changed the output type of each to “array. You can view the Simulink model in given image:
I have saved this model as “MATLAB_Ans.slx”. Using following MATLAB code, you can trace the steps to store the outputs in “output.xlsx:”.
% Run the Simulink model
simOut = sim('matlab_Ans18.slx');
% Get all logged variables except 'tout'
vars = setdiff(simOut.who, 'tout');
numVars = length(vars);
dataMatrix = [];
% Extract data from each variable
for i = 1:numVars
varData = simOut.get(vars{i});
dataMatrix = [dataMatrix, varData(:)];
end
% Convert to table and write to Excel
T = array2table(dataMatrix, 'VariableNames', vars);
writetable(T, 'output.xlsx');
disp('Data successfully written to output.xlsx');
After implementation “output.xlsx” will look like this:
For more clarification on the functions used, you can refer to the following resources,
  1. Sim: https://www.mathworks.com/help/simulink/slref/sim.html
  2. writetable: https://www.mathworks.com/help/matlab/ref/writetable.html
  3. Setdiff: https://www.mathworks.com/help/matlab/ref/double.setdiff.html
  4. Array2table: https://www.mathworks.com/help/matlab/ref/array2table.html
Or you can access release specific documentation using these commands in your MATLAB command window respectively:
web(fullfile(docroot, 'simulink/slref/sim.html'))
web(fullfile(docroot, 'matlab/ref/writetable.html'))
web(fullfile(docroot, 'matlab/ref/double.setdiff.html'))
web(fullfile(docroot, 'matlab/ref/array2table.html'))
Hope this solves your question!

Community Treasure Hunt

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

Start Hunting!