I am trying to randomize a marker color
23 次查看(过去 30 天)
显示 更早的评论
I have a graph that randomizes a point on the graph. I am also trying to randomize the marker color and edge color but then never change. He is what i have but I dont know how to fix it.
3 个评论
回答(2 个)
Maik
2022-11-7
编辑:Maik
2022-11-7
I assume the randi in your program was the problem. Here below we use it to generate a single
value everytime and pass it to the markersize plot handle.
close all
x = sin(0:2*pi/100:2*pi);
figure;
plot(x, '-b*');
markerSize = [1:10];
% randi syntax [1,10] range of numbers to choose the random value,
% 1,1 - number of random values to generate
rand_idx = randi([1,10],1,1);
randMarkerSize = markerSize(rand_idx);
figure;
h = plot(x, '-b*')
h.MarkerSize = randMarkerSize;
0 个评论
Walter Roberson
2022-11-8
'b':'r':'g'
You have used the colon operator. 'b' <= 'g' so you get at least one value, but 'b' + 'r' > 'g' so you only get one value, the 'b'
You perhaps wanted ['b', 'r', 'g'] -- which is exactly the same as 'brg' in MATLAB.
You randi(5) but 5 has no relationship to the number of marker colors you have to select from. You should numel() the color vector to find the number of items to select from. Then once you have your random number, use it to index the color array.
disp random_markerSize
Notice that displays the literal text random_markerSize . If you want to display the content of the variable random_markerSize then you need to use disp(random_markerSize)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!