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

조회 수: 5 (최근 30일)
Madhusudan
Madhusudan 2024년 7월 5일
댓글: Madhusudan 2024년 7월 8일
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
Madhusudan
Madhusudan 2024년 7월 5일
편집: Madhusudan 2024년 7월 5일
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 2024년 7월 5일
편집: Aquatris 2024년 7월 5일
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

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

답변 (1개)

Garmit Pant
Garmit Pant 2024년 7월 5일
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
Madhusudan
Madhusudan 2024년 7월 8일
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 2024년 7월 8일
can we segment the signal into individual cycles to compute the RMS (Root Mean Square) value for each cycle?

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by