How to periodically change Simulink Output file name?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I am trying to periodically output data from Simulink using the "To File" block, and I would like to use the current date and time as the name for each output file. Currently, I am trying to periodically call a function which determines the real-world time, converts that to a string, and uses the set_param command to change the output file name accordingly.
function [Y, M, D, H, MN, S] = fcn()
coder.extrinsic('now');
coder.extrinsic('datevec');
coder.extrinsic('sprintf');
coder.extrinsic('set_param');
[Y, M, D, H, MN, S] = datevec(now,'HH:MM:SS.FFF');
date_time=[Y M D H MN S];
fname = sprintf('OutputFile_%04d_%02d_%02d_%02d_%02d_%02f.mat', date_time);
set_param('untitled/To File','Filename',fname);
end
Unfortunately, I am receiving the error message "Cannot change parameter 'File name: (Filename)' of 'untitled/To File' while simulation is running".
I have only been able to successfully use the set_param command while Simulink is not running, but I need to change my output file name during simulation. Is this possible? Is there a better way to approach this problem?
Thank you,
Drew
0 Commenti
Risposte (1)
Prateekshya
il 4 Ott 2024
Hello Drew,
You are encountering the error because changing the filename of the "To File" block during simulation in Simulink is not directly supported.
Alternatively, you can use a MATLAB function block to perform this operation by writing your own code. Here is a sample code for the same:
function saveData(u)
coder.extrinsic('now', 'datevec', 'sprintf', 'save');
persistent lastSaveTime;
if isempty(lastSaveTime)
lastSaveTime = -inf;
end
% Define the save interval (e.g., 1 second)
saveInterval = 1; % seconds
% Get current time
currentTime = now;
[Y, M, D, H, MN, S] = datevec(currentTime);
timeString = sprintf('%04d_%02d_%02d_%02d_%02d_%02d', Y, M, D, H, MN, floor(S));
% Check if it's time to save data
if etime(datevec(currentTime), datevec(lastSaveTime)) >= saveInterval
% Create filename with timestamp
fname = sprintf('OutputFile_%s.mat', timeString);
% Save data to file
dataToSave = u; % Assuming 'u' is the input data to save
save(fname, 'dataToSave');
% Update last save time
lastSaveTime = currentTime;
end
end
To know more about MATLAB function blocks, kindly follow this link: https://www.mathworks.com/help/simulink/what-is-a-matlab-function-block.html.
I hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Prepare Model Inputs and Outputs in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!