Why does this matlab function throw an error on the first call and how do I prevent the error
1 次查看(过去 30 天)
显示 更早的评论
I am using the errorbar_tick function from https://www.mathworks.com/matlabcentral/fileexchange/22826-adjust-error-bar-width.
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
1 个评论
John D'Errico
2017-3-7
Why not use the debugger? After all, you were the one who made the changes, so you know what you did.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Errorbars 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!