How to load TreeBagger created and saved in an older version of MATLAB into a newer version?
Ältere Kommentare anzeigen
Hi,
I have trained a random forest model in MATLAB R2023b, and saved it as a TreeBagger. However, when I try to load it to MATLAB R2024b, I got this warning message: "Warning: Variable 'Mdl' originally saved as a TreeBagger cannot be instantiated as an object and will be read in as a uint32. "
Therefore, I cannot use this random forest model in MATLAB R2024b. Is there any way to fix this issue? Below is the code I used to save the trained model in MATLAB R2023b. Thanks a lot!
Yongli
save('C:\HKU\Research\Data\Random forest\Optimized models\model_opt_20241209.mat', 'Mdl');
2 Kommentare
Steven Lord
am 1 Jan. 2025
Are you trying to load this MAT-file in MATLAB or in a standalone application created using MATLAB Compiler?
If you are attempting to compile code using MATLAB Compiler that calls load to load an object from a MAT-file and there is no indication in the code itself that MATLAB Compiler needs to package the code, the dependency analyzer may not be able to detect that it needs to include the definition of the class. In this case use one of the workarounds in the "Callback Problems Due to Missing Functions" section of this documentation page, as this workflow is described by the Tip in that section of that documentation page.
Yongli
am 2 Jan. 2025
Akzeptierte Antwort
Weitere Antworten (1)
Manikanta Aditya
am 1 Jan. 2025
Hi @Yongli
It looks like you're encountering compatibility issues when loading a TreeBagger object saved in an older version of MATLAB into a newer version. This is a common issue when there are changes in the internal structure of objects between versions.
Here are a few steps you can try to resolve this:
- Use load with -mat Option: Ensure you are using the -mat option when loading the file. This can sometimes help with compatibility issues.
Mdl = load('C:\HKU\Research\Data\Random forest\Optimized models\model_opt_20241209.mat', '-mat');
- Update the Object: After loading the object, you might need to update it to the new version. MATLAB sometimes provides functions to update objects to the latest version. Check the documentation for any such functions.
- Save and Load Using Structs: Save the TreeBagger object as a struct and then convert it back to a TreeBagger object after loading. This can sometimes bypass compatibility issues.
% Save as struct
MdlStruct = struct(Mdl);
save('model_opt_20241209_struct.mat', 'MdlStruct');
% Load and convert back to TreeBagger
loadedStruct = load('model_opt_20241209_struct.mat', '-mat');
Mdl = TreeBagger(loadedStruct.MdlStruct);
Refer to the following MATLAB answer post to know more about the issue:
- Error loading TreeBagger from .MAT file in deployed app. - MATLAB Answers - MATLAB Central
- Using function of newer MATLAB version in older version - MATLAB Answers - MATLAB Central
I hope this helps.
1 Kommentar
Yongli
am 2 Jan. 2025
Kategorien
Mehr zu Classification Ensembles finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!