Sample rate in Analog Input/Output blocks in Simulink

1 回表示 (過去 30 日間)
Anna
Anna 2025 年 2 月 12 日
回答済み: Avni Agrawal 2025 年 3 月 20 日
I am preparing a class assignment with NI myDAQ where the sample rate should be a variable to be defined by the students.
Sample rate should be a variable in the workspace so students can change it according to results from previous computations on the system itself.
If I try to insert a variable, defined in the workspace as a positive number, I get error. Is there a workarounf for it?
  2 件のコメント
Manikanta Aditya
Manikanta Aditya 2025 年 2 月 12 日
What is the error you encountered?
Anna
Anna 2025 年 2 月 12 日
If I try to enter N as Sample rate in the block parameters (N exists in the workspace and N=50) I get the message that: "Sample rate must be specified as a positive real number".

サインインしてコメントする。

回答 (1 件)

Avni Agrawal
Avni Agrawal 2025 年 3 月 20 日
Hi Anne,
I understand that you are trying to use NI myDAQ with Simulink, setting the sample rate as a variable from the MATLAB workspace can sometimes cause issues. Here's how you can work around this:
1. Define the Variable Before running your model, ensure the variable is defined in the workspace. For example:
N = 50; % Sample rate, can be changed by students
2. Use a Constant Block: Instead of directly entering the variable into block parameters, use a Constant block:
  • Drag a Constant block into your model.
  • Set its value to `N`.
  • Connect it to the block where the sample rate is needed.
3. MATLAB Function Block: If you need more flexibility, use a MATLAB Function block:
  • Add a MATLAB Function block to your model.
  • Inside the function, use `N` to set the sample rate.
function y = setSampleRate(N)
y = N; % Use N as the sample rate
end
4. Parameterize the Model: Ensure your model is set up to use workspace variables:
  • Go to `Simulation` > `Model Configuration Parameters`.
  • Check the `Data Import/Export` settings to allow workspace variables.
By following these steps, you can successfully use a workspace variable as the sample rate in your Simulink model with NI myDAQ.
I hope this helps!

カテゴリ

Help Center および File ExchangeSignal Attributes and Indexing についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by