データを列ごとに配列にして読み込むスクリプト

27 visualizzazioni (ultimi 30 giorni)
Momoko Ito
Momoko Ito il 18 Set 2020
Risposto: Kojiro Saito il 18 Set 2020
質問です。CSVファイルを読み込む際、データを列ごとに配列にして読み込むスクリプトがわかりません。
おしえていただけますか。

Risposte (1)

Kojiro Saito
Kojiro Saito il 18 Set 2020
一番簡単な方法は、MATLABでcsvファイルをダブルクリックしてインポートウィザードを起動し、出力タイプを「列ベクトル」に変更して、「選択のインポート」で「データのインポート」をクリックします。
これでCSVファイルの各列がそれぞれ別の変数として格納されます。
また「選択のインポート」から「スクリプトの生成」や「関数の生成」をクリックすれば、同じことを行うMATLABコードが自動生成されます。
もし列が多くてcol1=t.col1のようなコードを全列分書くのが大変という場合は、下記のようにassigninを使うことで、動的に列名の変数を格納されます。
t = readtable('mydata.csv');
for n=1:width(t)
%(t.Properties.VariableNames{n}) = t.(t.Properties.VariableNames{n});
assignin('base', (t.Properties.VariableNames{n}), t.(t.Properties.VariableNames{n}));
end

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!