How can I create a multiple fill with different colors using loops for an arbitrary vector

Hi all, I am not very advance in matlab so forgive me if I mix up the terms. I understand the basics of how to use the fill function fill(X,Y,C). From matlab documentation, it can also be used for multiple fills say fill(X1,Y1,C1,X2,Y2,C2,...).
I have a vector
X = [1 2 2 2 4 4 5 5 3 3 3 1 1 1]
and
Y = [0 1 1 0 0]
Nz = linspace(0,1,length(X))
dz = max(Nz)/length(X)
I want to write a for loop to create a multiple fill for each identical successive element with different colors for example
x1 = [0 dz*count dz*count 0 0]
Y = [0 1 1 0 0]
x2 = [dz*count dz*count2 dz*count2 dz*count dz*count]...
Y = [0 1 1 0 0]
C1,C2...
fill(x1,Y,C1,x2,Y,C2...).
"count" here determines the number of times successive elements occur in the vector. "dz" is the step size. I would be glad if anyone here can assist. Thank you.

3 Comments

In all likelyhood a loop will not be necessary.
However, I don't understand how x1, x2, etc. relate to X, nor what count, or count2 is.
Thank you for your response Guillaume. Sorry for not been clear enough. Here, I want to scale the elements of X on Nz. So for the first element of X that is 1, it has a scaled value of 1*dz = 0.0714. the 1 multiplying dz is just the count of 1 until 2. Similary, for the second element of X that is 2, it occurs three times in succession so we have 3*dz = 0.214 and for the fifth element of X that is 4, it occurs twice in succession so we have 2*dz = 0.1428 and it continous in that fashion. So in this case, x1 = [0 1*dz 1*dz 0 0], x2=[1*dz 3*dz 1*dz 1*dz]. It continuous in that fashion until the last successive elements. So in the end we ought to have something like as shown in the image. where the width of each color in the Nz direction coresponds to the calculations above and the color determined from the elements of X itself( for example the first and last elements of X are the same (1) so they have the same color). I hope this helps clarify the question.
in the image.
And the goal is to be able to write a function that can handle an arbitray vector array in the same manner

Sign in to comment.

 Accepted Answer

Hello, Guillaume, I want to thank you so much for your assistance throughtout this thread. It has been really invaluable. Also thank you Stephen Cobeldick. With regards to the last question, with some experimentation, I was able to use a for loop to loop through like so
fill(xpoints(:,ii), ypoints(:,ii),colormap(colour(ii,:)))
and got what I was looking for. Thank you very much.

More Answers (1)

As suspected a loop is not required:
X = [1 2 2 2 4 4 5 5 3 3 3 1 1 1];
seqlengths = diff([0, find(diff(X)), numel(X)]); %calculate the length of each continuous sequence
colour = X(cumsum(seqlengths)); %extract the colour corresponding to each sequence
xpoints = repelem(cumsum([0 seqlengths(1:end-1); seqlengths], 2), 2, 1); %build xpoints matrix
ypoints = [0 1 1 0].';
fill(xpoints, ypoints, colour);
But as pointed out by Stephen, using image is even simpler.

3 Comments

Interesting. Thank you very much Guillaume. But just a quick follow up because of the so many unfamiliar functions you used in there. With this method,can I define my own custom color range (say from white through gray to black where low X element (1) corresponds to white and high to black(5)) using the colormap function already available in matlab? Those colors I used were just dummy colors I used to carry my thought through. Also, I want to be able to normalize the x axis to Nz as defined above because I would ultimately lay a plot over this background that would have the x-axis value to be Nz = ([0 max(Nz)]) and y = ylim([0 1]). As I said, it is a function that can require any arbitrary vector.
The colour parameter is unchanged from your original code. It is an index into the colour map. You can use any colour map you wish.
You can multiply the xpoints coordinates by any factor you want.
xpoints = xpoints / numel(X) %to scale x axis to [0 1]
In order to understand better the code I've written, I would recommend you break it down and see what each function does. For example
  • diff(X) calculates the difference between consecutive elements of X. It's therefore going to be 0 for consecutive identical elements and non-zero otherwise.
  • find(diff(X)) thus returns the indices at which the X values change
  • the diff of that is thus the length of each sequence
  • etc.
Hello Guillaume, thank you for your help so far. Correct me if I am wrong. If I have n number of xpoints, to define a custom colormap, they should also be C1,C2...Cn. In our case, we have 6 xpoints right?. I define a custom colormap set as in the matrice below. colour = [ 1.0000 1.0000 1.0000; 0.5918 0.5918 0.5918; 0.7113 0.7113 0.7113; 0.5000 0.5000 0.5000; 0.2778 0.2778 0.2778; 0.4444 0.4444 0.4444 ] When I call it as fill(xpoints, ypoints,colour), I get the error, Error using fill Vectors must be the same length. What am I doing wrong?

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!