How do I generate a variable in work space for the sample time which Simulink uses for simulation to be used in a code?

조회 수: 4 (최근 30일)
clc
format short
%tout: a vector of number. The below removes numbers that are not multiple of 0.00005
tol = 1e-14; %abritrary tolerance below which numbers are considered equal
tout(abs(mod(tout, 0.00005)) > tol) = [];
tout = unique(tout);
NS1 = size(tout); % to find the number of samples
NS2=NS1(1,1);
FileName = input('Please enter a file name:', 's');
FN_cfg = [FileName,'.cfg'];
FN_dat = [FileName,'.dat'];
Data =[transpose((1:1:NS2)) tout M];
csvwrite(FN_dat,Data)
i want to get the sample time which simulink use in
tout(abs(mod(tout, 0.00005)) > tol) = [];
in place of 0.00005 so that it will become user defined and not hard coded value.
can anyone help me with this?

답변 (1개)

Shivaprasad Narayan
Shivaprasad Narayan 2020년 2월 14일
It is my understanding that you are trying to find the number of samples based on the Solver sampling time of the Simulink model. You would like to get this sample time from what is set in the model, rather than fixing it to particular value.
What you can do is:
1. Get the sample time of model using the function "get_param". This returns a character array.
2. Convert it to a numeric value and use it in the code.
Your code might look similar to the below piece of code:
SampleTime = str2num(get_param(<Model Name>,'FixedStep')); % Get solver sample time and convert it to number
Use the variable "SampleTime" in place of 0.00005.
Hope this helps!
  댓글 수: 3
Shivaprasad Narayan
Shivaprasad Narayan 2020년 2월 20일
<Model Name> should be replaced by your model name.
e.g. if you have named your model as "Example_model.slx" then the command would look like below
SampleTime = str2num(get_param(Example_model.slx,'FixedStep')); % Get solver sample time and convert it to number
When you use variable step, the simulink sample time is not fixed. You can refer to the documentation page. It explains how to chose solvers.
Tejas Rivonkar
Tejas Rivonkar 2020년 2월 25일
but sir i don't want to change the model name each time i run the code for different models. is there a way that the model name will be selected directly when required?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by