GUI: Change backround of "Edit text" box using if structure

7 ビュー (過去 30 日間)
Nigel
Nigel 2012 年 11 月 26 日
Hello! In my GUI I have two inputs and output (they are "Edit text" boxes) and a button which executes a function. As I said, the final output goes to Edit Text box. I want to analyze this output with if structure, and, depending on the value, assign different background color for the box.
I tried using set(hObject,'BackgroundColor','color'), (example below) And it worked..
function box_CreateFcn(hObject, eventdata, handles)
set(hObject,'BackgroundColor','color')
But when it is inside IF structure, MATLAB returns many errors and code doesn't work (see code below)
function box_CreateFcn(hObject, eventdata, handles)
if value <-30
set(hObject,'BackgroundColor','blue');
elseif value>=-30
set(hObject,'BackgroundColor','grey');
elseif value<=100
set(hObject,'BackgroundColor','black');
else
set(hObject,'BackgroundColor','red');
end
What am I doing wrong? :)
  1 件のコメント
Jan
Jan 2012 年 11 月 26 日
When ever you mention, that errors appear, it is a good idea to post the messages also.

サインインしてコメントする。

採用された回答

Arthur
Arthur 2012 年 11 月 26 日
Explain 'many errors'. How can we fix many errors with we don't know what they are?? I'm going to make an estimated guess. If your code is exactly as you showed here, than you forgot to get the string of the textbox. Include this above your if.
value = str2double(get(hObject,'String'))
Also, the way you programmed your if's is not going to work; it will never reach the last two options. Note that matlab always goes to the first if that is true, and value will always be <-30 or >=-30....
  2 件のコメント
Nigel
Nigel 2012 年 11 月 26 日
編集済み: Nigel 2012 年 11 月 26 日
numbers in if structure are given just for example, don't pay attention to that :)
Arthur
Arthur 2012 年 11 月 27 日
編集済み: Arthur 2012 年 11 月 27 日
Well, when even the code in your question isn't the correct one, I don't know how we can help you. You'll have to give us some more info (=errors & code).
But I see that this code is in the CreateFcn. Shoudn't it be in the Callback?

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2012 年 11 月 26 日
編集済み: Image Analyst 2012 年 11 月 26 日
Have you tried to use the debugger? Set a breakpoint and see if it actually gets into any of the "if" conditions that you list.
And what is "value" - have you assigned that yet?
And you can try using handles.box instead of hObject.
  1 件のコメント
Nigel
Nigel 2012 年 11 月 26 日
value is the number, calculated using function

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by