How to perform a for loop over a single excel column

1 回表示 (過去 30 日間)
Maaya
Maaya 2019 年 1 月 9 日
編集済み: Guillaume 2019 年 1 月 9 日
I'm trying to change the color my scatter plot points depending on a 3rd column with a for loop that would iterate over the column. Here is my code:
data=xlsread('datafile.xlsx');
x = data(:,2);
y = data(:,3);
z = data(:,4);
for k = drange(length(z))
if z(k) == -2
c = [1,0,0];
sz = 100;
else
c = [0,1,0];
sz = 10;
end
end
scatter(x,y,sz,c, 'filled')
Any help?
  4 件のコメント
Maaya
Maaya 2019 年 1 月 9 日
I can't share data file due to the nature of the data/source project, but it's organized into three columns. The first two columns are coordinates which I would plot, and the third column would change the color of the point (for example, if the value is -3 then the code would make that particular point larger and the color red). The values in the third column are either 1,0, -1,-2,-3. If it doesn't match a value, then the point would stay a smaller size and green.
Maaya
Maaya 2019 年 1 月 9 日
drange is probably an error on my part. I'm trying to loop through the length of the 3rd column

サインインしてコメントする。

回答 (1 件)

Guillaume
Guillaume 2019 年 1 月 9 日
編集済み: Guillaume 2019 年 1 月 9 日
I don't see the point of a loop. Simply use your 3rd column to construct a Nx3 matrix of colours:
colours = [1, 0, 0 ... -3
0, 0, 1 ... -2
0.5 0.5 1 ... -1
0, 1, 0 ... 0
0.2 .4 .6]; % 1
sizes = [100 ... -3
50 ... -2
20 ... -1
10 ... 0
5]; % 1
data = xlsread('datafile.xlsx')
rowindex = data(:, 4) + 4; %rescale from -3...1 to 1...5. For more generic code, use the 2nd output of ismember
rowsize = sizes(rowindex);
rowcolour = colours(rowindex, :);
scatter(data(:, 2), data(:, 3), rowsize, rowcolour, 'filled');

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by