、あなたは自動的に変数LastName
、Sex
を生成する場合は、Age
等することができます:
- 変数の名前を取得します。テーブルの
Properties.VariableNames
プロパティに格納されます。
- ID彼らは問題がに単一varaiblesの名前の作成に今から構成データにアクセスするテーブルの列(varaibles)を介しvaraibleリストの長さ
- ループ
として数字entifyワークスペースにデータを格納します。
フィールドの名前を動的に生成できるようにデータをstruct
に保存することで、この問題を解決できます。
したがって、テーブル内の変数の名前の後にフィールドの名前を生成することができます。
提案されたアプローチの可能な実装が可能colud:
hospital_data =
row_names: {100x1 cell}
LastName: {100x1 cell}
Sex: [100x1 nominal]
Age: [100x1 double]
Weight: [100x1 double]
Smoker: [100x1 logical]
BloodPressure: [100x2 double]
Trials: {100x1 cell}
ホープこのことができます、
Qapla」
:
% Load the data
load hospital
% Convert dataset to table
T = dataset2table(hospital)
% Get the name of the varaibles
var_names=T.Properties.VariableNames
% Identify the number of varaibles (to be used in the next loop)
n_var=length(var_names)
% Store the data in a struct
% Get the name of the rows
hospital_data.row_names=T.Properties.RowNames;
% Loop over the variables
% Create dynamically the names of the fields of the struct after the name
% of the variables
for i=1:n_var
hospital_data.(var_names{i})=T{:,i}
end
このstruct
そのフィールドですhospital_data
という名前を生成します