필터 지우기
필터 지우기

Write file(s) with all properties of a MATLAB model

조회 수: 1 (최근 30일)
the cyclist
the cyclist 2022년 8월 31일
댓글: Walter Roberson 2022년 8월 31일
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)
ans = 3×1 cell array
{'BinEdges' } {'CategoricalPredictors'} {'CategoricalSplit' }
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
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?
Walter Roberson
Walter Roberson 2022년 8월 31일
struct() and jsonencode()?

댓글을 달려면 로그인하십시오.

답변 (1개)

Abderrahim. B
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
  댓글 수: 1
the cyclist
the cyclist 2022년 8월 31일
편집: the cyclist 2022년 8월 31일
I appreciate the response, but I don't think you quite understood the depth of the issue. Maybe I didn't give enough detail.
Yes, the property names are a cell, and it would be trivial to write that cell to file, using either the generic fprintf function or a more specific one like writecell.
But that is not what I am doing. Rather, I need to write the underlying data values for each property. For example, I want to write the property values for "PredictorNames"
mdl.('PredictorNames')
ans =
1×5 cell array
{'x1'} {'x2'} {'x3'} {'x4'} {'x5'}
which is itself a cell array, but other property values might be a numeric array:
mdl.('X')
ans =
0.5377 0.6715 -0.1022 -1.0891 1.4193
1.8339 -1.2075 -0.2414 0.0326 0.2916
-2.2588 0.7172 0.3192 0.5525 0.1978
0.8622 1.6302 0.3129 1.1006 1.5877
...
So, via your method, I am still stuck with writing a separate format specification for each property, which is exactly what I am trying to avoid.
(I truly think there is no way around this, but a guy can hope.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by