Issue "Found unsupported class for variable using function 'coder.load'."
Mostrar comentarios más antiguos
Model.mat includes two griddedInterpolant objects, with spline inter- and extrapolation.
function [T_out, V_out] = my_interpolant_eval(A,B,C)
% persistent speichert den Interpolant im Speicher
%
persistent F1;
persistent F2;
if isempty(F1)
data = coder.load('Model.mat');
F1 = data.T_interp;
F2 = data.V_interp;
end
T_out = F1(A,B,C);
V_out = F2(A,B,C);
end
in Codegeneration -->
Found unsupported class for variable using function 'coder.load'.
Value at 'data.T_interp' unsupported for code generation.
Use the command 'whos -file Model.mat' to view the variables in the MAT file.
Not sure what needs to be changed, so it's working.
4 comentarios
If you use the command
whos -file Model.mat
to view the variables in the MAT file, what do you get as output ?
Hi,
I have tried to create a simple example to reproduce this and it worked fine. Could you provide more information?
- A sample Model.mat file.
- The context in which you try to use this. I was guessing a MATLAB Function block of a Simulink model, but I might be wrong.
Best wishes,
Harald
Harald
hace alrededor de 2 horas
Hi again,
I had tried in R2026b and only realized that you are working in R2024a, sorry for this. I could reproduce the problem in R2024a..
If you can move to R2026b, this would be the easiest solution.
In R2024a, I tried a similar approach to what Torsten suggested and it worked fine.
If neither is suiting your needs, please consider contacting Technical Support: https://de.mathworks.com/support/contact_us.html
Best wishes,
Harald
Respuestas (1)
Answer from AI:
This error occurs because coder.load cannot reconstruct an object instance (like griddedInterpolant) directly from a MAT file during code generation. [1]
Even though the griddedInterpolant class itself supports C/C++ code generation, coder.load is strictly meant to load raw compile-time data constants (such as structures, matrices, and vectors). It treats objects/classes as an unsupported format. [1]
🛠️ The Fix: Separate Data from the Object
To resolve this, you must only save the raw grid points and values inside your .mat file, load them via coder.load, and then instantiate the griddedInterpolant object inside your code. [1]
Step 1: Save only the raw arrays to your MAT file
Instead of saving the entire griddedInterpolant object, save its properties (GridVectors and Values): [1]
matlab
% In your MATLAB base workspace script:
F = griddedInterpolant(X, Y, Z, V, 'linear', 'linear');
% Extract raw data arrays
grid_x = F.GridVectors{1};
grid_y = F.GridVectors{2};
grid_z = F.GridVectors{3};
vals = F.Values;
% Save ONLY the matrices/vectors
save('ModelData.mat', 'grid_x', 'grid_y', 'grid_z', 'vals');
Step 2: Reconstruct the Object in your Codegen Function
Modify your function to use persistent variables so the object is only created once (during the first call), loading the raw values via coder.load: [1]
matlab
function V_out = my_interpolant_eval(xq, yq, zq)
% Keep the object instance in memory across calls
persistent F;
if isempty(F)
% 1. Load ONLY the raw numeric vectors/matrices
data = coder.load('ModelData.mat');
% 2. Re-instantiate the griddedInterpolant inside the code
% Note: 'linear' and extrapolation methods must be compile-time constants!
F = griddedInterpolant({data.grid_x, data.grid_y, data.grid_z}, data.vals, 'linear', 'linear');
end
% Evaluate the interpolant
V_out = F(xq, yq, zq);
end
Categorías
Más información sobre Interpolation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!