I am trying to randomize a marker color
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1184248/image.png)
3 Kommentare
Antworten (2)
Maik
am 7 Nov. 2022
Bearbeitet: Maik
am 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);
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 Kommentare
Walter Roberson
am 8 Nov. 2022
'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 Kommentare
Siehe auch
Kategorien
Mehr zu Discrete Data Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!