How to import excel data to simin block as input and export output to excel through simout block?

6 views (last 30 days)
How to import excel data to simin block as input and export output to excel through simout block? in=xlsread(filename,c,'temp','b2') out=xlswrite('temp.xls'), is it correct??

Answers (1)

Shubham
Shubham on 21 Aug 2024
Hi venkatatejeshreddy,
To import excel data to “from workspace" block (simin) and export output to excel through "to workspace" block (simout), you can use the help of model "Callbacks" which can be accessed from Modelling Tab -> "Model Properties".
For importing excel data to "from workspace" block, you can use the model's "PreLoadFcn". Here's is an example pre-load function that can be used to import excel data to "from workspace" block:
% Read data from the Excel file
filename = 'random_data.xlsx';
data = readmatrix(filename, 'Sheet', 'Sheet1');
% Extract time and signal
time = data(:, 1);
signal = data(:, 2);
% Prepare data for the Simin block
simin = [time, signal];
% Assign to base workspace
assignin('base', 'siminData', siminData);
For exporting excel data from "to workspace" block, you can use model's "StopFcn". Here's is an example of simulation stop function that can be used to export data to excel sheet:
% Retrieve the simulation output from the workspace
outputData = evalin('base', 'simout');
% Define the filename for the Excel file
outputFilename = 'simulation_output.xlsx';
% Since the output is an array, assume the first column is time
outputMatrix = outputData;
% Write the simulation output to the Excel file
writematrix(outputMatrix, outputFilename, 'Sheet', 'Sheet1', 'WriteMode', 'overwrite');
% Inform the user
disp(['Simulation output written to ', outputFilename]);
Please note that you need to configure your "Save format" according to your requirements from "Block Parameters" of "To Workspace" block.
Hope this helps.

Categories

Find more on Data Import from MATLAB 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!