Search code examples
matlabhistogrammatlab-figure

How to find x-value of highest peak in histogram?


How to save x-value of highest peak in histogram into variable? Thank you enter image description here


Solution

  • fileID = fopen('q65.txt','r');
    formatSpec = '%f';
    file = fscanf(fileID,formatSpec);
    
    h = histogram(file,50);
    %Find index of maximum
    [~, index]= max(h.Values);
    
    delta = h.BinLimits(2)-h.BinLimits(1);
    % find the range for a single bin
    slot = delta./h.NumBins;
    
    %location = minimum y + (index of maxmium)*slot 
    lb = h.BinLimits(1) + (index-1)*slot;
    ub = h.BinLimits(1) + (index)*slot;
    
    location = [lb, ub]
    

    Location is a range, not a fixed number

    A simple one

    fileID = fopen('q65.txt','r');
    formatSpec = '%f';
    file = fscanf(fileID,formatSpec);
    h = histogram(file,50);
    %Find index of maximum
    [~, index]= max(h.Values);
    lb = h.BinEdges(index);
    ub = h.BinEdges(index+1);
    location = [lb, ub]