Sample rate in Analog Input/Output blocks in Simulink

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 commentaires

What is the error you encountered?
Anna
Anna le 12 Fév 2025
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".

Connectez-vous pour commenter.

Réponses (1)

Avni Agrawal
Avni Agrawal le 20 Mar 2025

0 votes

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!

Catégories

En savoir plus sur Communications Toolbox dans Centre d'aide et File Exchange

Produits

Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by