フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

if [0 3 5 10] then first text box should be green, all the rest red. If [3 0 5 10] then second text box should be green. In following code, the color is not changing with change in number.

1 回表示 (過去 30 日間)
kavya p
kavya p 2015 年 1 月 31 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
function fig2n(x,figure1)
figure1=figure;
red=[1 0 0];
green=[0 1 0];
yellow=[1 .8 0];
% drawnow
x=[3 0 5 10];
if x(1)==0<=x(1)<=2
light1=green;
elseif x(2)==3<=x(2)<=4
light1=red;
elseif x(3)==5<=x(3)<=6
light1=red;
elseif x(4)==7<=x(4)<=10
light1=red;
end
if x(1)==3<=x(1)<=4
light2=red;
elseif x(2)==5<=x(2)<=6
light2=red;
elseif x(3)==7<=x(3)<=10
light2=red;
elseif x(4)==0<=x(4)<=2
light2=green;
end
if x(1)==5<=x(1)<=6
light3=red;
elseif x(2)==7<=x(2)<=10
light3=red;
elseif x(3)==0<=x(3)<=2
light3=green;
elseif x(4)==3<=x(4)<=4
light3=red;
end
if x(1)==7<=x(1)<=10
light4=red;
elseif x(2)==0<=x(2)<=2
light4=green;
elseif x(3)==5<=x(3)<=6
light4=red;
elseif x(4)==3<=x(4)<=4
light4=red;
end
annotation(figure1,'textbox',...
[0.38 0.46 0.03 0.067],...
'String',{x(1)},...
'FitBoxToText','on','FontWeight','bold',...
'EdgeColor','none','BackgroundColor',[1 1 1],'Color',light1);
annotation(figure1,'textbox',...
[0.48 0.35 0.03 0.067],...
'String',{x(2)},...
'FitBoxToText','on','FontWeight','bold',...
'EdgeColor','none','BackgroundColor',[1 1 1],'Color',light2);
annotation(figure1,'textbox',...
[0.58 0.46 0.03 0.067],...
'String',{x(3)},...
'FitBoxToText','on','FontWeight','bold',...
'EdgeColor','none','BackgroundColor',[1 1 1],'Color',light3);
annotation(figure1,'textbox',...
[0.48 0.57 0.03 0.067],...
'String',{x(4)},...
'FitBoxToText','on','FontWeight','bold',...
'EdgeColor','none','BackgroundColor',[1 1 1],'Color',light4);
end
  2 件のコメント
John D'Errico
John D'Errico 2015 年 1 月 31 日
I formatted your code for your to make it readable. Please do so yourself in the future.
Image Analyst
Image Analyst 2015 年 1 月 31 日
Please read this. Also, you might like to use control-a control-i in the MATLAB editor to clean up the formatting before you paste it in here.

回答 (1 件)

John D'Errico
John D'Errico 2015 年 1 月 31 日
Please learn to use the code formatting button. Your code will be unreadable otherwise, and it takes only one click of the mouse to do!
To answer your question...
if x(1)==0<=x(1)<=2 light1=green;
This, while it is executable in MATLAB, does NOT do what you think it does. You think of that as a set of equalities and inequalities.
In fact, what MATLAB does is parse it as...
if ((x(1)==0)<=x(1))<=2 light1=green;
So, MATLAB does the first test, i.e., (x(1)==0).
The result of that will be either true(1) or false(0). Then it compares that numeric value to x(1). Again, the test will be either true(1) or false(0), which it then compares to the number 2.
But both of 0 or 1 will ALWAYS be less than the number 2. So no matter what x is, those tests will ALWAYS evaluate to true!
Instead, I think you wanted to write this as:
if (x(1)==0) && (0<=x(1)) && (x(1)<=2), light1=green;
The same applies to all of your other if statements. Just because you tend to use a shorthand notation for mathematics does not always mean that MATLAB will understand that notation.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by