How to load TreeBagger created and saved in an older version of MATLAB into a newer version?
1 view (last 30 days)
Show older comments
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 Comments
Steven Lord
on 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.
Accepted Answer
Walter Roberson
on 1 Jan 2025
Warning: Variable 'explainer' originally saved as a shapley cannot be instantiated as an object and will be read in as a uint32.
That message can indicate that the class definition for shapley is not present. There are two possibilities for that:
- You might not have installed the appropriate toolbox in your R2024b installation (perhaps you do not have it licensed either)
- you might have the right toolbox installed and licensed, but the class definition for shapley might no longer exist due to a code rearrangement
However... toolbox/stats/classreg/+classreg compared between R2023b and R2024b shows that there are new files in R2024b but all of the old files from R2023b still exist, so it is not the case that a class was reorganized out of existence
On the other hand, I do not find class or function shapely anywhere in R2023b (I have most toolboxes but not all of them), so possibly shapely is a custom class you created
More Answers (1)
Manikanta Aditya
on 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.
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!