Write file(s) with all properties of a MATLAB model
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Bjorn Gustavsson
il 31 Ago 2022
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?
Risposte (1)
Abderrahim. B
il 31 Ago 2022
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
1 Commento
Vedere anche
Categorie
Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!