Azzera filtri
Azzera filtri

How can get the code in c by Matlab coder ?

3 visualizzazioni (ultimi 30 giorni)
CL P
CL P il 5 Dic 2023
Dear Expert,
I want to convert ANN model to C code file. I develpoed a ANN model that predict one output correspondin to some input, so we have weight, bias, network and input parameters. could you suggest me how can i convert this ANN model to C code by Matlab coder.
I tried tp convert it but error is coming: 1.spreadsheetimportoption is not supported for code generation 2. setvaropts is not supported 3. readable is not supported 4. training is not supported 5. The test is not supported 6. fitness is not supported (3) Additionally, when I attempt to upload the Matlab function in "c coder," the input parameter needs to be manually selected, as the coder is not automatically extracting it from the code.
Code is:
function Nu = NUprediction(Re, Pr, alpha_g)
double Re,
double Pr,
double alpha_g,
NUprediction = readtable("C:\Users\hp\Desktop\Code_VTU_Read\Final_Nu\New folder\NU_prediction.xlsx", opts, "UseExcel", false);
M= NUprediction;
Input=table2array([M(:,1) M(:,2) M(:,3)]);
Output=table2array(M(:,4));
c = cvpartition(length(Output),"Holdout",0.20);
trainingIdx = training(c); % Indices for the training set
XTrain = Input(trainingIdx,:);
YTrain = ((Output(trainingIdx)));
testIdx = test(c); % Indices for the test set
XTest = (Input(testIdx,:));
YTest = (Output(testIdx));
%%
Mdl = fitrnet(XTrain,YTrain,"Standardize",true, "LayerSizes",[30 30 40 10]);
%%
testMSE = loss(Mdl,XTest,YTest)
%%
testPredictions = predict(Mdl,XTest);
%%
trainMSE = loss(Mdl,XTrain,YTrain)
trainPredictions = predict(Mdl,XTrain);
%%
CFD=M;
Input_CFD=table2array([M(:,1) M(:,2) M(:,3)]);
Output_CFD=table2array([M(:,4)]);
% Predict the output
predicted_output = predict(Mdl, Input_CFD);
  1 Commento
Walter Roberson
Walter Roberson il 4 Gen 2024
double Re,
double Pr,
double alpha_g,
MATLAB itself does not have declarations of variables.
Those lines are not illegal, but they do not do anything useful. Those lines are equivalent to
double('Re')
double('Pr')
double('alpha_g')
which cause the internal character codes to be displayed -- so you would get stray
82 101
80 114
97 108 112 104 97 95 103
displayed every time the function was executed.

Accedi per commentare.

Risposte (1)

Venu
Venu il 4 Gen 2024
Modificato: Venu il 4 Gen 2024
Hi @CL P,
In the given code, functions like 'readtable', 'spreadsheetimportoption', 'setvaropts' are not supported for code generation. You need to load your data outside of the MATLAB function and pass it directly as an argument to the function.
Machine learning functions such as 'fitrnet', 'cvpartition', 'loss', and 'predict' are not directly supported for code generation. You need to export the trained model using 'savecompactmodel' and then write a custom prediction function using the model's weights and biases.
In a brief, to proceed with code generation, you would need to:
  1. Train your ANN model outside the code generation process.
  2. Save the trained model parameters (weights and biases) in a format that you can use in a custom prediction function.
  3. Rewrite the 'NUprediction' function to accept the trained model parameters and input data directly, without performing any unsupported operations.
  4. Implement the prediction logic manually, using matrix operations that are supported by MATLAB Coder.
The input arguments 'Re', 'Pr', and 'alpha_g' for 'NUprediction' function are indeed defined with their types specified as double. However, in the context of MATLAB Coder, you need to provide additional information about the size and shape of the inputs when they are arrays or matrices.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by