Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with plotting with string of different color names

Status
Not open for further replies.

bluemood

Specifier/Regulator
Dec 11, 2002
5
Hello,

I am having problem with using different colors base on the the various cmd in the for loop. I apply the suggested solution provided in one of the past threads but I am not able to get the result. I don't understand what the error message is reporting. Can someone help me? Many Thanks in advance.

C=['r.' 'g.' 'b.' 'c.'];
for ht = 0:(num-1)
for cmd = 1:4,
idx = (find(new_h == ht & new_cmd == cmd));
[r] = ind2sub(idx, [idx]);
chs = [new_cmd, new_c, new_s];
cmdgroup = chs([r],:);
c = cmdgroup:),2);
s = cmdgroup:),3);
cmd = cmdgroup:),1);
plot(c, s, C(cmd));
axis ([-5000 40000 -100 900]);
hold on;
end;
end;


======================================================
Error message :

??? Error in color/linetype argument

On line 49 ==> plot(c, s, C(cmd));
 
Replies continue below

Recommended for you

I see a combination of two problems causing this error

1) The loop index variable 'cmd' is altered inside the loop (line 10 of your code). Is this correct?


2) Matlab interprets

Code:
C=['r.' 'g.' 'b.' 'c.'];

as

Code:
C = ['r.g.b.c.'];

so C(1) is 'r', C(2) is '.', C(3) is 'g' etc.

One way around this is to replace
Code:
plot(c, s, C(cmd));
with
Code:
plot(c, s, C((cmd-1)*2+1:cmd*2));

A more rigorous way is to replace the string C with a cell array of strings and convert each element in the cell array to a string as required (using the 'char' command, ie:
Code:
C = {'r.' 'g.' 'b.' 'c.'};
(note the curly braces)
and then plot using
Code:
plot(c, s, char(C(cmd)));

Hope this helps

M
 
Hi MikeP,

Your suggested solution works.

Problem 1) is solved by changing to another name. typo error.

Problem 2) is solved by adapting to 2nd method.

Thank you very much. Have a Happy Christmas and Happy New Year 2003 to you & all forum users.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor