Clear Filters
Clear Filters

Getting error while using .m files in MATLAB function block in simulink.

50 views (last 30 days)
Hello,
I am currently working on creating a custom RMS block within MATLAB's Simulink environment.
My goal is to replicate the functionality of the standard RMS block using a MATLAB function.
I have developed an .m file (attached) that calculates the RMS value. However, I am encountering difficulties in implementing this code within the Simulink Function block.
Could anyone assist me in resolving this issue?
thank you
-Madhusudan reddy
  3 Comments
Madhusudan
Madhusudan on 5 Jul 2024 at 8:34
Edited: Madhusudan on 5 Jul 2024 at 9:13
output is unexpected.
I need MATLAB Function Block that i can replace with original RMS Block.
because orginal RMS Block we can not use for HDL code genration.
Aquatris
Aquatris on 5 Jul 2024 at 9:55
Edited: Aquatris on 5 Jul 2024 at 9:58
and do you copy paste the code you attached to your question to the Simulink matlab function block?
If so, it is not in the right format. That block expects a function in the form shown below, where x and t becomes input ports and y and z becomes the output port of the matlab function block. You can define however many inputs and outputs to your function by simply adding or removing arguments
function [y,z]=myFun(x,t)
%% stuff
end

Sign in to comment.

Answers (1)

Garmit Pant
Garmit Pant on 5 Jul 2024 at 9:14
Hello Madhusudan
To replicate the functionality of the ‘RMS’ block in Simulink using a MATLAB function, you need to use the Simulink’s MATLAB Function Block.
First, you need to edit your M-File to work as a function. Following code snippet has the necessary edits for your code to run with Simulink.
function a = call_rms()
amplitude = 240*sqrt(2); % Amplitude of the sine wave
frequency = 60; % Frequency of the sine wave in Hz
phase = 0; % Phase of the sine wave in radians
duration = 1; % Duration of the sine wave in seconds
sampling_rate = 1e6; % Sampling rate in Hz
% Generate the time vector
t = 0:1/sampling_rate:duration;
% Calculate cycle parameters
cycle_period = 1/frequency;
num_cycles = duration/cycle_period;
num_samples_per_cycle = cycle_period * sampling_rate;
% Generate the sine wave
y = amplitude * sin(2*pi*frequency*t + phase);
% Calculate RMS (adjusting the loop)
rms_ary = zeros(1,round(num_samples_per_cycle*num_cycles));
rms_mean = zeros(1,round(num_samples_per_cycle*num_cycles));
rms = zeros(1,round(num_cycles)); % Store RMS for each cycle
for i = 1:round(num_cycles)
start_index = (i-1)*num_samples_per_cycle + 1;
end_index = min(i*num_samples_per_cycle, length(y)); % Ensure we don't go out of bounds
rms_ary = y(round(start_index):round(end_index)).^2;
rms_mean(i) = mean(rms_ary);
rms(i) = round(sqrt(rms_mean(i)));
end
a = rms(1);
end
Save the file as ‘call_rms.m’.
In Simulink, insert the MATLAB Function block and double click on it to configure it. Add the following code snippet to the code editor of the MATLAB function block:
function y = rms_custom()
y = call_rms();
Running this model will simulate the function of RMS block. You can make changes to the ‘call_rms.m’ file to generate different sine waves.
You can also refer to the following MATLAB Answers post to resolve some commonly faced errors while using the MATLAB Function block workflow:
I hope you find the above explanation and suggestions useful!
  4 Comments
Madhusudan
Madhusudan on 8 Jul 2024 at 5:13
I am currently designing a Ground Fault Current Interruption (GFCI) simulation model. This GFCI sensor will measure the differential current (i.e., the difference between the current entering the load and the current leaving the load) and compare it with a predefined threshold value. For this purpose, I need to calculate the RMS (Root Mean Square) current.
I have successfully implemented the GFCI sensor module using the RMS block from Simulink. However, I encountered an issue when generating HDL (Hardware Description Language) code from the Simulink model because the Simulink RMS block is not compatible with HDL.
To resolve this issue, I am planning to replace the RMS block with a MATLAB Function Block.
Madhusudan
Madhusudan on 8 Jul 2024 at 7:50
can we segment the signal into individual cycles to compute the RMS (Root Mean Square) value for each cycle?

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!