When using MATLAB functions like fitrgp within a MATLAB Function block in Simulink, you must handle the fact that Simulink does not natively support MATLAB objects like RegressionGP. Instead, you need to use "coder.extrinsic" to declare the function as extrinsic, meaning it will be executed in the MATLAB workspace (which you have done). However, the MATLAB Function block cannot directly handle non-numeric data types returned by extrinsic functions.
Since the output GPMdl is a MATLAB object, you cannot use it directly in Simulink. Instead, you can encapsulate the entire process of fitting the Gaussian Process Regression model and extracting any necessary numeric data for use in Simulink into a custom MATLAB function. This function can then be called as an extrinsic function from within the MATLAB Function block in Simulink. Refer the code snippet below:
function predictedOutput = fitAndExtractGPR()
% Example inputs
myinput = [rand(100,1) rand(100,1)];
myoutput = rand(100,1);
% Parameters for fitrgp
sigma0 = 1e-5;
sigmaF0 = 1e-5;
d = 2;
sigmaM0 = 1e-2 * ones(d, 1);
% Fit the Gaussian Process Regression model
GPMdl = fitrgp(myinput, myoutput, 'Basis', 'constant', 'FitMethod', 'exact', ...
'PredictMethod', 'exact', 'KernelFunction', 'ardsquaredexponential', ...
'Optimizer', 'QuasiNewton', ...
'KernelParameters', [sigmaM0; sigmaF0], 'Sigma', sigma0, 'Standardize', 1);
% Extract some numeric data from the model
% For example, extract the predicted responses for a new set of inputs
newInput = [rand(10,1) rand(10,1)];
predictedOutput = predict(GPMdl, newInput);
end
In your Simulink model, use the MATLAB Function block with the following code:
function y = fitGPRModel(~)
%#codegen
% Declare the custom function as extrinsic
coder.extrinsic('fitAndExtractGPR');
% Initialize output
y = zeros(10, 1); % Adjust size based on what fitAndExtractGPR returns
% Call the custom function
predictedOutput = fitAndExtractGPR();
% Assign the output
y = predictedOutput;
end
This could be one way to achieve your objectives.