Finding top point in two gaussians - matlab

So i have this graph with these two gaussians created in matlab
This Graph have been created using the following bit of matlab code
TimeTakenWDriver = textread('TimeTakenWDriver.txt');
TimeTakenWODriver = textread('TimeTakenWODriver.txt');
fig1 = figure;
h1 = histfit(TimeTakenWDriver);
std1 = std(TimeTakenWDriver);
std2 = std(TimeTakenWODriver);
mean1 = mean(TimeTakenWDriver)
mean2 = mean(TimeTakenWODriver)
delete(h1(1));
set(h1(2),'color','b');
hold on;
h2 = histfit(TimeTakenWODriver);
delete(h2(1));
set(h2(2),'color','r');
Now i want to find the two y coordinates that corresponds to the two means "the two top-points" ive searched around but cant get any of the solutions i find to work because of the way i create my graphs. Any ideas?

You can try getting the data directly from the plot:
C = get(get(gca, 'Children'), 'YData');
and then find the peak value using max:
max(C(:))
Note that if you have two or more plots on the same axes (as in your example), C would be a cell array, so you need to access each cell separately to get the peaks:
cellfun(#(x)max(x(:)), C)

Related

Finding where two histograms cross paths - MATLAB

I am generating two histograms using the histogram function from Matlab that are both normalized using the probability argument.
However, once I generate two histograms as shown below, I'd like to be able to find the exact point at which the histograms would cross paths, assuming the histograms were drawn using lines instead of bars. Unfortunately, this form of histogram doesn't allow for lines, it just has bars. There is a hist function which can be manipulated in Matlab to draw a histogram as lines instead of bars, however, it doesn't easily normalize.
Hence, ideally, I'd like to use histogram() to plot the 2 histograms and find where they cross. See image below:
Here's an example of how the graphs can be created:
x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;
Now from here, I want to find the point where the two histograms cross paths. Note, the intersection value is the intersection (in the mathematical sense). This is not what I'm looking for. I'm looking for the x coordinate of where the two histograms cross at their outer boundaries. For example, in the attached image, the answer would be ~2.5.
From your example data, with a simple modification:
x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h1.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];
h2.Normalization = 'probability';
h2.BinWidth = 0.25;
h2.BinLimits=[min([x(:); y(:)]) max([x(:); y(:)])];
data1=h1.Values;
data2=h2.Values;
intersection_value=find(data2>data1,1); % this is the index, bad variable name

Plotting data labels within lines in Matlab

My question is similar to the post: matlab curve with label
I have some data (acquired using a function too long to show here), which gives me 2 arrays: Nv4 (337x1) and t (337x1) and I want to plot 'a=40' on the plot line.
I should be able to use contour label, but I need to convert my data as matrix format first. The post above gives a link to explain how to convert our data, unfortunately the link has expired and I have no idea how I should convert my data to. An example would be useful !
I'm posting this as a new question because I don't have enough reputation to post a comment
I guess there is another way, simply with text. Here is a sample:
% Create a sample curve
x = 1:337;
y = sqrt(x);
plot(x,y);
% Define position to display the text
i = round(numel(x)/2);
% Get the local slope
d = (y(i+1)-y(i))/(x(i+1)-x(i));
X = diff(get(gca, 'xlim'));
Y = diff(get(gca, 'ylim'));
p = pbaspect;
a = atan(d*p(2)*X/p(1)/Y)*180/pi;
% Display the text
text(x(i), y(i), 'a=40', 'BackgroundColor', 'w', 'rotation', a);
And here is the result:
Best,

Multiple colormaps using bar(z,'stacked')

I have read the article recommended by a post on another colormap thread http://www.mathworks.com/matlabcentral/answers/101346 and I understand the concept. I am having trouble understanding the values of CDATA when using the bar(z,'stacked') function.
I have one figure with a major axis plotted using cmap, and I have created and positioned a new axis for the bar chart and I want it to use cmap2.
For example, my code includes:
maps = colormap([cmap;cmap2]);
bH = bar(z,'stacked');
Where z = 25x10 (annual data for 10 years over 25 sites)
Now when I look at the CDATA
get(bH,'CDATA') A cell array is returned of size 1x10 with each cell containing the string 'scaled'.
Now if I look at the CDATA of each of the children
childH = get(bH,'children');
get(childH{i},'CDATA')
A matrix of size 25x10 is returned with every value equal.
e.g. childH{i}'s CDATA is a matrix of size 25x10 having all values = i
So how can I scale these to map to my colormap since
from the documentation above I need to perform:
m = size(colormap,1); % Number of colors in the current colormap
Data = get(H,'CData') % Where H is a handle to a surface or patch object
cmin = min(CData(:)); % Minimum color value
cmax = max(CData(:)); % Maximum color value
idx = min(m,round((m-1)*(CData-cmin)/(cmax-cmin))+1);
idx becomes min(m,nan) which is always m?
I really need help understanding this.
Am I missing something or is this function a special case?
First make sure that cmap2 has exactly the number of colors that you want to use, then get the barseries object to map into it directly. Something like:
childH = get(bH, 'children');
for a = 1:numel(childH)
C = get(childH{a}, 'FaceVertexCData');
C(:) = a+size(cmap, 1);
set(childH{a}, 'FaceVertexCData', C, 'CDataMapping', 'direct');
end

How to save measured pixel distance data in matlab

I got this code from here
figure, imshow('pout.tif');
h = imdistline(gca);
api = iptgetapi(h);
fcn = makeConstrainToRectFcn('imline',...
get(gca,'XLim'),get(gca,'YLim'));
api.setDragConstraintFcn(fcn);
Can you tell me how i can save the pixel distance data measured between two points "dynamically" into a separate file or as a variable in matlab
figure, imshow('pout.tif');
h = imdistline(gca);
api = iptgetapi(h);
fcn = makeConstrainToRectFcn('imline',...
get(gca,'XLim'),get(gca,'YLim'));
api.setDragConstraintFcn(fcn);
dist = api.getDistance()
I believe you are looking for how to use the API. In the above example, you call api followed by the functions on the page you listed. I.e.
api.getDistance
api.getAngleFromHorizontal
etc

Simple MATLAB Graph Plotting

This may be an obviously simple question, but i'm not sure how to do this.
I have 4 calculated values stored in 4 variables, each representing a condition. I want to simply display each of these in a graph with the condition/variable on the X axis and the values on the Y axis. I have tried the code below, but it just gives me a blank figure with values but no line.
figure(1)
T = TA;
S = SA;
U = UA;
O = OA;
plot(T,S,U,O, '--o')
shg
Thanks in advance.
Try this
figure(1)
T = 12;
S = 7;
U = 5;
O = 10;
plot([T,S,U,O], '--o');
set(gca,'XTick',[1,2,3,4]);
set(gca,'XTickLabel',{'T','S','U','O'})
shg
for me this gave
Check matlab help on plot and specifically linespecs to define the format you would like the data plotted in. For example, code below plots each variable in different color with line and symbol.
figure;
hold on;
plot(T,'-bo');
plot(S,'-g.');
plot(U,'-rd');
plot(O,'-mx');