How can I specify in my routine that in p=plot(v, M) curves should be colored by pairs?

Dear all,
I am new in matlab and I want to plot the values from matrix "M" vs values from vector "v" as following:
p=plot(v, M); In this case I obtain a family of curves that have as a mirror y=0.075.
My question is: how can I specify in my routine that the curves should be colored by pairs? e.g. the two curves closer to y=0.075 (in both sides of y=0,075)should be green, the two next closer curves should be red etc...
Any ideas? thanks in advance

Answers (1)

I guess that you have to decide at first, which curves are "closer to y=0.0075" at first - is this correct?
lineH = plot(v, M);
dist = sum(abs(M - 0.075), 2); % or along the 1st dimension?!
[dummy, index] = sort(dist);
lineH = lineH(index); % Sort the line handles according to their distance
set(lineH(1:2), 'Color', [0, 1, 0]);
set(lineH(3:4), 'Color', [1, 0, 0]);

1 Comment

Thanks Jim for your support,
Since v and M are sorted from lowest to highest I know that the row in the midle of v and M is y=0.075. Every row above the mentioned "y=0,075" is farther from it (same for the rows below). In this case the first and last rows are the ones that have more distance to y=0,75.
I probably had a mistake in my first comment. I didnt mentioned that I had a loop to go over every column of M and treat it as a vector Mv. So is Plot (v,Mv) and both are vectors.
I tried your sugestion and I got an error. Here is the code and the error.
Code
figure(2)
hold on
for i=1:length(alf) % number of rows for M
for j=1:length(V) % number of colums for M
Mv(j)=M(i,j); % I take every colum from M to Mv
end
lineH=plot(V, Mv);
lineH=lineH(i);
set(lineH(1:2), 'Color', [0, 1, 0], 'LineStyle', '-');
set(lineH(3:4), 'Color', [1, 0, 0], 'LineStyle', '-');
end
The error:
??? Index exceeds matrix dimensions.
Error in ==> dibIRRbeta at 23 set(lineH(1:2), 'Color', [0, 1, 0], 'LineStyle', '-');
Error in ==> gui_LPVR>Start_pushbut_Callback at 121 dibIRRbeta (eio, handles);
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> gui_LPVR at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)gui_LPVR('Start_pushbut_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback

This question is closed.

Asked:

on 24 Apr 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!