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

27 vues (au cours des 30 derniers jours)
Momoko Ito
Momoko Ito le 18 Sep 2020
質問です。CSVファイルを読み込む際、データを列ごとに配列にして読み込むスクリプトがわかりません。
おしえていただけますか。

Réponses (1)

Kojiro Saito
Kojiro Saito le 18 Sep 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

Catégories

En savoir plus sur MATLAB Coder dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!