Main Content

このページは前リリースの情報です。該当の英語のページはこのリリースで削除されています。

関数に cell 配列の内容を渡す

これらの例では、cell 配列を入力として認識しない MATLAB® の関数に、cell 配列からのデータを渡す方法をいくつか説明します。

中かっこ {} でインデックスを付けることにより、1 つの cell の内容を渡す

この例では、テキストを含んでいる cell 配列と乱数の 20 行 2 列の配列を作成します。

randCell = {'Random Data', rand(20,2)};
plot(randCell{1,2})
title(randCell{1,1})

Figure contains an axes object. The axes object with title Random Data contains 2 objects of type line.

内容にさらにインデックスを付けること (マルチレベルのインデックス) により、データの最初の列のみをプロットします。

figure
plot(randCell{1,2}(:,1))
title('First Column of Data')

Figure contains an axes object. The axes object with title First Column of Data contains an object of type line.

cell2mat を使用して、複数の cell から数値データを結合する

この例では、3 つの都市の気温データを格納して、日付により各都市の気温をプロットする 5 行 2 列の cell 配列を作成します。

temperature(1,:) = {'2020-01-01', [45, 49, 0]};
temperature(2,:) = {'2020-04-03', [54, 68, 21]};
temperature(3,:) = {'2020-06-20', [72, 85, 53]};
temperature(4,:) = {'2020-09-15', [63, 81, 56]};
temperature(5,:) = {'2020-12-31', [38, 54, 18]};

allTemps = cell2mat(temperature(:,2));
dates = datetime(temperature(:,1));

plot(dates, allTemps)

Figure contains an axes object. The axes object contains 3 objects of type line.

複数の cell の内容をコンマ区切りリストとして関数に渡す

この例では、Y に対して X をプロットし、2 行 3 列の cell 配列 C からライン スタイルを適用します。

X = -pi:pi/10:pi;
Y = tan(sin(X)) - sin(tan(X));

C(:,1) = {'LineWidth'; 2};
C(:,2) = {'MarkerEdgeColor'; 'k'};
C(:,3) = {'MarkerFaceColor'; 'g'};

plot(X, Y, '--rs', C{:})

Figure contains an axes object. The axes object contains an object of type line.

関連するトピック