필터 지우기
필터 지우기

Output of Function in Form of 'Color',[0 0.2 0.4]!

조회 수: 2 (최근 30일)
Ingo Hermann
Ingo Hermann 2017년 11월 1일
편집: Adam 2017년 11월 1일
Dear Community, I want to write a Function clrs('color') which returns 'Color',[0 0.2 0.4] as an argument so that I can do plot(x,y,clrs('green')); I tried it with str = ' ''Color'',[0 0.2 0.4] '; and with eval but it doesn't work. Do you have any idea how i can do that?
function [out] = clrs(color)
if (strcmp('blue',color) == 1)
value = [0 0.447 0.741];
end
out{1} = 'Color';
out{2} = value;
end
plot([1 2],[1 2],'x',clrs('blue'));
  댓글 수: 1
Adam
Adam 2017년 11월 1일
Off the top of my head I don't think that is possible. To pass two distinct arguments to the plot function they would have to be in e.g a cell array as
args = { 'color', [0 0.2 0.4] };
Then you can pass
plot( ..., args{:} )
but I don't think there is any syntax that allows you to return multiple arguments out of a function and pass them to another. You cannot chain a {:} on the end of a function call either unless I am much mistaken.

댓글을 달려면 로그인하십시오.

답변 (4개)

M
M 2017년 11월 1일
clrs('blue')
returns
ans =
'Color' [1x3 double]
so you cannot use it directly in the plot command.
First you need to access :
b=clrs('blue')
b =
'Color' [1x3 double]
and then get the value
b{2}
ans =
0 0.4470 0.7410
But you will not be able to use it directly in the plot command, see the plot documentation .

Ingo Hermann
Ingo Hermann 2017년 11월 1일
Thank you for your fast response :D Then I have to write all the time 'Color' in front of my function...

Steven Lord
Steven Lord 2017년 11월 1일
One alternative would be to write a function named setcolor that accepts a handle of the line you plotted and sets its Color property.
setcolor = @(h, newcolor) set(h, 'Color', newcolor);
h = plot(1:10, 1:10);
pause % so you can see the original color
setcolor(h, 'r')
For simple demonstration purpose I wrote a basic setcolor as an anonymous function. You can create a more powerful and more complicated version as a regular function file.

Adam
Adam 2017년 11월 1일
편집: Adam 2017년 11월 1일
Well, you can do:
colourArgs = clrs('blue')
plot([1 2],[1 2],'x', colourArgs{:});
if you are willing to accept two lines of code!

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by