Write file(s) with all properties of a MATLAB model
3 次查看(过去 30 天)
显示 更早的评论
Suppose I have built a machine learning model in MATLAB. It has a list of properties (and underlying property data) that encapsulates the model. For example ...
% Set seed for reproducibility
rng default
% Number of observations and features
N_obs = 20;
N_feat = 5;
% Generate some made-up data
x = randn(N_obs,N_feat);
y = randn(N_obs,1);
% Fit a model, and get the properties
mdl = fitrtree(x,y);
modelProperties = sort(properties(mdl));
% Display a few properties
modelProperties(1:3)
I would like to share the property values (not just the property names) with a non-MATLAB user. I'm looking for a clever way to write the values to a text file.
The main stumbling block is that the properties are of many different data types, so I cannot just loop through them and use a single function (e.g. writecell) to write to file. It seems I will need to hard-code a switch statement or the like, to find the appropriate function to write each data type appropriately.
Am I overlooking a more elegant method?
2 个评论
Bjorn Gustavsson
2022-8-31
Maybe not "elegant" as such, but I instantly thought of the header of fits-files. They contain meta-data with key-word -> meta-data, where the meta-data can be of "very arbitrary" types. As I recall there are good tools for adding meta-data of different types. If that doesn't solve your problem exactly there has to be some other similar tools already handling this. Surely you shouldn't need to re-invent this functionality in 2022?
回答(1 个)
Abderrahim. B
2022-8-31
Hi!
I prefer to use fprintf with fopen and fclose. Once you learn how to design formatSpecifications you will always use fprintf instead of other data writing functions.
You have a cell,, suggest to convert to table, then you write row by row.
Hope this helps
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!