Filter löschen
Filter löschen

Why does this matlab function throw an error on the first call and how do I prevent the error

1 Ansicht (letzte 30 Tage)
I have made some minor modifications to make it work with the 2016a version. This function throws an error the first time I call it, stating that: Index exceeds matrix dimensions. Error in errorbar_tick (line 48) x = h.Bar.VertexData(1,:); On a repeated call, it works. What is going on here and how can I fix this problem?
The modified code, in its entirety, is below.
function errorbar_tick(h,w,xtype)
%ERRORBAR_TICK Adjust the width of errorbars
% ERRORBAR_TICK(H) adjust the width of error bars with handle H.
% Error bars width is given as a ratio of X axis length (1/80).
% ERRORBAR_TICK(H,W) adjust the width of error bars with handle H.
% The input W is given as a ratio of X axis length (1/W). The result
% is independent of the x-axis units. A ratio between 20 and 80 is usually fine.
% ERRORBAR_TICK(H,W,'UNITS') adjust the width of error bars with handle H.
% The input W is given in the units of the current x-axis.
%
% See also ERRORBAR
%
% Author: Arnaud Laurent
% Creation : Jan 29th 2009
% MATLAB version: R2007a
%
% Notes: This function was created from a post on the french forum :
% http://www.developpez.net/forums/f148/environnements-developpement/matlab/
% Author : Jerome Briot (Dut)
% http://www.mathworks.com/matlabcentral/newsreader/author/94805
% http://www.developpez.net/forums/u125006/dut/
% It was further modified by Arnaud Laurent and Jerome Briot.
% Check numbers of arguments
narginchk(1,3)
% Check for the use of V6 flag ( even if it is depreciated ;) )
flagtype = get(h,'type');
% Check number of arguments and provide missing values
if nargin==1
w = 80;
end
if nargin<3
xtype = 'ratio';
end
% Calculate width of error bars
if ~strcmpi(xtype,'units')
dx = diff(get(gca,'XLim')); % Retrieve x limits from current axis
w = dx/w; % Errorbar width
end
% Plot error bars
if strcmpi(flagtype,'errorbar') % ERRORBAR(...)
x = h.Bar.VertexData(1,:); % Retrieve bar xdata from errorbar
dp = length(x)/3; % 3 data points per error bar
m = 1; % Multiplier for addition/subtraction
for ii = 1:dp
m = -1*m; % Switch between subtraction and addition
x(dp+ii:dp:end) = x(ii)+m*w/2; % Change xdata with respect to the chosen ratio
end
h.Bar.VertexData(1,1:end) = x;
else
error('Please enter an ErrorBar object!');
end

Akzeptierte Antwort

Joshua Knicely
Joshua Knicely am 7 Mär. 2017
The answer is in the comments of this question: https://www.mathworks.com/matlabcentral/answers/264534-how-can-i-change-the-width-of-horizontal-lines-of-error-bars-in-errorbar-plot-in-matlab-2015b
According to Lamia Kasmi:
" Thanks, indeed it works in the command window, however it doesn't in a script. The solution to that is that one needs to add a "drawnow" after calling the errorbar function and before calling the errorbar_tick function, otherwise errorbar_tick might be faster than the plotting and it might look for the handle before it is even created by the errorbar function. So in summary, in a script, one should use errorbar_tick in the following way:
Example:
x = [1 2 3 4];
y = [1 2 1 2];
dy = [0.1 0.1 0.1 0.1];
h = errorbar(x,y,dy);
drawnow
errorbar_tick(h) "

Weitere Antworten (0)

Kategorien

Mehr zu Errorbars finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by