Change color of the output string in GUI by command

13 views (last 30 days)
I want to change the color of the output string by using popup menu. I have the command below but it seems to have some problems. Can anyone help me please.
function popupcolor_Callback(hObject, eventdata, handles)
% hObject handle to popupcolor (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupcolor contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupcolor
switch get(handles.popupcolor, 'text')
case 1
set (handles.test_text, 'ForegroundColor', Red);
case 2
set (handles.test_text, 'ForegroundColor', Yellow);
case 3
set (handles.test_text, 'ForegroundColor', Purple);
case 4
set (handles.test_text, 'ForegroundColor', Blue);
case 5
set (handles.test_text, 'ForegroundColor', Green);
otherwise
end

Answers (1)

Voss
Voss on 19 Dec 2021
As far as I know there is no 'text' property of uicontrols. I think you mean to get the 'Value' property. That's one problem.
The second problem may be that Red, Yellow, etc., are not recognized variables or functions (or maybe they are functions you wrote - I cannot know), but I suspect you mean to use character color specifications (i.e., 'r', 'y', etc.). If this is the case, then you can try writing the callback like this:
function popupcolor_Callback(hObject, eventdata, handles)
% hObject handle to popupcolor (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupcolor contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupcolor
switch get(handles.popupcolor, 'Value')
case 1
set (handles.test_text, 'ForegroundColor', 'r');
case 2
set (handles.test_text, 'ForegroundColor', 'y');
case 3
set (handles.test_text, 'ForegroundColor', 'm'); % magenta is like purple, right?
case 4
set (handles.test_text, 'ForegroundColor', 'b');
case 5
set (handles.test_text, 'ForegroundColor', 'g');
otherwise
end
Or, if you want to be more flexible in case the set of colors listed in the popupmenu changes in the future (or the order changes, or the set of colors becomes something the user can control), you can avoid tying the behavior of the callback to the Value of the popupmenu and go by the text of the selected popupmenu item (like the Hints GUIDE puts there say). The approach below works for the common colors MATLAB supports specifying as a character ('r' 'g' 'b' 'c' 'm' 'y' 'k' 'w') and I've put in an exception to treat 'Purple' as magenta ('m'):
function popupcolor_Callback(hObject, eventdata, handles)
% hObject handle to popupcolor (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupcolor contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupcolor
contents = cellstr(get(handles.popupcolor,'String'));
str = contents{get(handles.popupcolor,'Value')};
if strcmp(str,'Purple')
str = 'Magenta';
end
set(handles.test_text, 'ForegroundColor', lower(str(1)));

Community Treasure Hunt

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

Start Hunting!