0

I'm trying to create an histogram with a normal distribution fitting. I wrote the code but it keeps on saying: index exceeds matrix dimension. Here's the code:

sigma=handles.Ht(end);
        figure(1)
        x_min = min(handles.array);
        x_max = max(handles.array);
        x_full = linspace(x_min,x_max,100);
        x_partial = x_full(x_full < L);
        y_full = normpdf(x_full,mean(handles.array),sigma);
        y_partial = normpdf(x_partial,mean(handles.array),sigma);
        area(x_full,y_full,'FaceColor','b');
        hold on;
        area(x_partial,y_partial,'FaceColor','r');
        grid on;
        % Histogram data
        [count,bins] = hist(handles.array,30);
        % Scale bins
        num_returns = numel(handles.array);
        scale = (bins(2)-bins(1))*num_returns;
        % Plot full data set
        a = bar(bins,count/scale,'w');
        set(get(a,'Children'),'FaceAlpha',0.8)
        hold off;
        title(['Best Normal Fit of Returns. Red Indicates Returns Below VaR: ',num2str(L)],'FontWeight','bold');
        handles.matrici.Mdati=[handles.array, handles.Ht, handles.VaR_p];
        guidata(handles.figure1, handles);

Matlab says the error is the "area" function but I don't understand why. Any Ideas?

Index exceeds matrix dimensions.

Error in area (line 98)
            set(h(1),'RefreshMode','auto');

Error in Var_final>inv_quantile (line 643)
        area(x_partial,y_partial,'FaceColor','r');

Error in Var_final>pushbutton_calcola_Callback (line 309)
inv_quantile(handles)

Error in gui_mainfcn (line 96)
        feval(varargin{:});

Error in Var_final (line 42)
    gui_mainfcn(gui_State, varargin{:});

Error in
@(hObject,eventdata)Var_final('pushbutton_calcola_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback
5
  • Did you accidentally create a variable called area earlier in the code?
    – Dan
    Commented Mar 5, 2014 at 15:44
  • Thank you for your answer. This is part of a GUI I didn't create it.
    – Fodex
    Commented Mar 5, 2014 at 15:56
  • Then please provide the full error and the exact line at which it occurs
    – Dan
    Commented Mar 5, 2014 at 15:56
  • you can find it now on the main post
    – Fodex
    Commented Mar 5, 2014 at 15:58
  • N = 1 100 M = 1 100
    – Fodex
    Commented Mar 5, 2014 at 16:08

1 Answer 1

1

I get the same error by providing area with empty vectors as inputs, I have a feeling that that is what you are doing somehow:

area([], [])

I suggest you print size(x_partial) and size(y_partial) to the screen to debug this.

2
  • N = 1 100 M = 1 100
    – Fodex
    Commented Mar 5, 2014 at 16:10
  • 1
    Sorry I see the error is actually with the next area line, so probably x_partial = x_full(x_full < L); returns an empty matrix
    – Dan
    Commented Mar 6, 2014 at 6:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.