Write file(s) with all properties of a MATLAB model
3 views (last 30 days)
Show older comments
Commented: Walter Roberson on 31 Aug 2022
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
% 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
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?
Abderrahim. B on 31 Aug 2022
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
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!