Domanda

I am trying to change the color of a graph I created in MATLAB GUIDE. I initially set the colors of the x-axis and y-axis to be white in GUIDE but whenever I output to the graph, the both axes change color back to the default which is black. This is the code I am using to output my graph :

axes(handles.axes1); % axes1 is the tag for my graph in GUIDE
time = 0:1/fs:(length(handles.x)-1)/fs;
plot(time,handles.x);
title('Original Signal');

I looked at MATLAB documentation but did not find an example related to my problem since it is not the actual graph I want the color change but the x and y axis. Is this possible?

È stato utile?

Soluzione

Some Research

Thanks to OP’s question I have learnt something new today. So, I knew that one can change the XColor of the axes by code too, with the command – set(handles.axes1,'XColor',colorname), where colorname could be any color, like ‘r’ for red, ‘k’ for black, etc. So, I would have suggested to use set(handles.axes1,'XColor',’w’), but then one has to remember what XColor was set through GUIDE and copy over that information to the associated M-file, which isn’t “elegant enough”.

My initial idea was to extract that XColor info from Fig-file at the initialization stage of the GUI, which is at GUINAME_ OpeningFcn, save it into the handles structure and use it later on in callback functions through the handles again. I tried to call color_data = get(handles.axes1,'XColor') at the end of this function to get the XColor info as a three element vector. That didn’t work as it was giving me [1 1 1] which represents the default color white.

Next obvious thing was to put the color_data extracting command at the start of GUINAME_ OpeningFcn, before handles.output is set inside that function and it worked! Thus, the conclusion is that handles.output = hObject is putting at least Color information to default values.

Back to work

For OP, the steps would be following :

Put this at the start of GUINAME_ OpeningFcn

axes1_XColor = get(handles.axes1,'XColor');
axes1_YColor = get(handles.axes1,'YColor');

handles.axes1_XColor = axes1_XColor;
handles.axes1_YColor = axes1_YColor;

set(handles.axes1,'XColor',handles.axes1_XColor);
set(handles.axes1,'YColor',handles.axes1_YColor);

When plotting inside any function that has handles as one of the inputs, use the following commands after plot

set(handles.axes1,'XColor',handles.axes1_XColor);
set(handles.axes1,'YColor',handles.axes1_YColor);

Thus, for your example it would be –

axes(handles.axes1); % axes1 is the tag for my graph in GUIDE
time = 0:1/fs:(length(handles.x)-1)/fs;
plot(time,handles.x);
title('Original Signal');

set(handles.axes1,'XColor',handles.axes1_XColor);
set(handles.axes1,'YColor',handles.axes1_YColor);

Let’s hope it works for you, let us know!

Altri suggerimenti

Calling high level plotting functions like plot() resets the axes properties to their defaults. To preserve your custom properties, use the line() function instead:

axes(handles.axes1);
time = 0:1/fs:(length(handles.x)-1)/fs;
line(time,handles.x);
title('Original Signal'); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top