I am trying to randomize a marker color

14 views (last 30 days)
Amanda
Amanda on 7 Nov 2022
Answered: Walter Roberson on 8 Nov 2022
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 Comments
Amanda
Amanda on 7 Nov 2022
I have a graph and there is a point with a marker color I need the color of the marker to change everytime

Sign in to comment.

Answers (2)

Maik
Maik on 7 Nov 2022
Edited: Maik on 7 Nov 2022
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);
x = 1×101
0 0.0628 0.1253 0.1874 0.2487 0.3090 0.3681 0.4258 0.4818 0.5358 0.5878 0.6374 0.6845 0.7290 0.7705 0.8090 0.8443 0.8763 0.9048 0.9298 0.9511 0.9686 0.9823 0.9921 0.9980 1.0000 0.9980 0.9921 0.9823 0.9686
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 =
Line with properties: Color: [0 0 1] LineStyle: '-' LineWidth: 0.5000 Marker: '*' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 … ] YData: [0 0.0628 0.1253 0.1874 0.2487 0.3090 0.3681 0.4258 0.4818 0.5358 0.5878 0.6374 0.6845 0.7290 0.7705 0.8090 0.8443 0.8763 0.9048 0.9298 0.9511 0.9686 0.9823 0.9921 0.9980 1 0.9980 0.9921 0.9823 0.9686 0.9511 0.9298 0.9048 0.8763 … ] Show all properties
h.MarkerSize = randMarkerSize;

Walter Roberson
Walter Roberson on 8 Nov 2022
'b':'r':'g'
ans = 'b'
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
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)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!