Clear Filters
Clear Filters

How to call a function containing large dataset of inputs and target attribute in matlab function block

3 views (last 30 days)
function y=RefGen(u1,u2)
[u1,u2,yd]=dsdata();
x=[u1 u2];
Here u1 and u2 are inputs and y is the output,
dsdata() is the function to be called in the matlab function block
u1 and u2 will assign the input vectors containing 100 samples each and yd will assign the target attribute
x is the input matrix containing u1 and u2 inputs
  4 Comments
Aquatris
Aquatris on 13 Jun 2024
Edited: Aquatris on 14 Jun 2024
Since you mention dataset, is dsdata a '.mat' file or a function that outputs the G T MPPT arrays?
% if dsdata is a mat file with G T MPPT variable inside
function [u1,u2,yd]=RefGen(u1,u2)
data = load('dsdata.mat');
u1 = data.G;
u2 = data.T;
yd = data.MPPT;
end
% if dsdata is a function that outputs G T MPPT
function [u1,u2,yd]=RefGen(u1,u2)
[G,T,MPPT] = dsdata;
u1 = G;
u2 = T;
yd = MPPT;
end
Divye
Divye on 13 Jun 2024
Thanks for your answer. The RefGen(u1,u2) function is written in the matlab function block.But where the dataset or dsdata() should be written as each of G,T,MPPT contain 100 samples

Sign in to comment.

Answers (1)

Nipun
Nipun on 13 Jun 2024
Hi Divye,
I understand that you want to call a function dsdata that provides a large dataset of inputs and target attributes within a MATLAB Function block. Here's a concise example of how you can achieve this:
  1. Ensure dsdata is correctly defined and accessible within your MATLAB path.
  2. Modify your MATLAB Function block code to properly handle the large dataset.
Here's how you can structure your function:
function y = RefGen(u1, u2)
% Call the dsdata function to get input and target data
[u1, u2, yd] = dsdata();
% Combine inputs into a single matrix
x = [u1 u2];
% Here you can define how you want to process the inputs
% For demonstration, let's assume y is computed as the mean of the target
y = mean(yd);
end
Notes:
  1. dsdata Function: Ensure dsdata returns the dataset in the required format.
  2. Handling Large Data: If your dataset is extremely large, consider optimizing memory usage or processing data in chunks.
Example dsdata Function:
function [u1, u2, yd] = dsdata()
% Example dataset generation
u1 = rand(100, 1); % Replace with actual data
u2 = rand(100, 1); % Replace with actual data
yd = rand(100, 1); % Replace with actual target data
end
Including in MATLAB Function Block:
  1. Open your Simulink model.
  2. Insert a MATLAB Function block.
  3. Double-click the MATLAB Function block to open the editor.
  4. Replace the content with the RefGen function code above.
  5. Ensure dsdata is either in the same directory or in the MATLAB path.
For detailed guidance on using MATLAB Function blocks, refer to the MathWorks documentation: https://www.mathworks.com/help/simulink/ug/adding-data-to-a-matlab-function-block.html
Hope this helps.
Regards,
Nipun
  1 Comment
Divye
Divye on 13 Jun 2024
Thanks for your answer. i want to know that where to write the dsdata() function in the simulink and how to handle large dataset. I didn't get your 5th point and that you have written in notes. May you provide with some example.
The dataset contains dsdata()=[G T MPPT].where G and T are inputs and MPPT is the target attribute. Each of G,T,MPPT contain 100 samples .

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!