Changing the histogram colour from within another function

Hi,
I have a histogram function "myHistogram" that I want to be able to change the colour of the bars from another function.
function [ctrs,counts,mode]=myHistogram(handles,data,step,colour)
edges=[0:step:max(data(:))-1];
h=histogram(data,edges,'EdgeAlpha',0.3,'EdgeColor',colour);
setappdata(0,'histog',h)
I thought that saving the handles structure h using setappdata would allow it to be called in the other function by using getappdata.
function logXAxis(source,event,ax,handles)
val = source.Value
h=getappdata(0,'histog')
if val==1
set(gca,'XScale','log')
xlabel('Log(Intensity)')
h.FaceColor = [.850 0.3250 0.0980];
else
set(gca,'XScale','linear')
xlabel('Intensity')
h.FaceColor = [0 0.4470 0.7410];
end
However, nothing happens, no change in colour.

4 Kommentare

I would try adding the handle to the histogram to the handles structure.
Have you tried the debugger to check step by step what happens? That usually helps me finding my more stupid mistakes (a missing minus or abs or something like that).
Jason
Jason am 22 Feb. 2017
Bearbeitet: Jason am 22 Feb. 2017
But why is it not already passed in the handles structure?
In fact it is, If I print out h when its called in the second function it gives me:
Data: [40401×1 uint16]
Values: [1×1176 double]
NumBins: 1176
BinEdges: [1×1177 double]
BinWidth: 50
BinLimits: [25 58825]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0 0 1]
So it must be something to do with setting the faceColor I guess
Adam
Adam am 22 Feb. 2017
Bearbeitet: Adam am 22 Feb. 2017
You say 'nothing happens'. First thing to check is always whether or not the histogram is being retrieved or not as there are (at least) 2 obvious totally different possibilities for why "nothing happens".
Either you don't get the histogram (which I would have thought would yield a syntax error) or you do get the histogram, but your instructions to change the colour don't achieve what you want.
It is always best to make sure you can pin down as tightly as possible the source of a bug rather than bring in a whole load of surrounding stuff that may or may not have anything to do with the bug.
In a quick test changing the face colour like that should work. Does the xlabel change? Is that function even getting called?
Your right it does work. I needed to do a refresh.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 22 Feb. 2017

Kommentiert:

am 22 Feb. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by