I deleted part of the trapz function code, how do I fix it?
4 views (last 30 days)
Show older comments
Hi, so I had an error with trapz function and it specified line 51, so I opened the code. While looked at the file trapz.m, I accidently deleted part of the code in trapz.m. How do I fix this?
0 Comments
Answers (2)
Bora Eryilmaz
on 7 Dec 2022
Edited: Bora Eryilmaz
on 7 Dec 2022
Did Undo not work (Ctrl + Z)?
This might help you restore it. What version of MATLAB are you using?
function z = trapz(x,y,dim)
%TRAPZ Trapezoidal numerical integration.
% Z = TRAPZ(Y) computes an approximation of the integral of Y via
% the trapezoidal method (with unit spacing). To compute the integral
% for spacing different from one, multiply Z by the spacing increment.
%
% For vectors, TRAPZ(Y) is the integral of Y. For matrices, TRAPZ(Y)
% is a row vector with the integral over each column. For N-D
% arrays, TRAPZ(Y) works across the first non-singleton dimension.
%
% Z = TRAPZ(X,Y) computes the integral of Y with respect to X using the
% trapezoidal method. X can be a scalar or a vector with the same length
% as the first non-singleton dimension in Y. TRAPZ operates along this
% dimension. If X is scalar, then TRAPZ(X,Y) is equivalent to X*TRAPZ(Y).
%
% Z = TRAPZ(X,Y,DIM) or TRAPZ(Y,DIM) integrates across dimension DIM
% of Y. The length of X must be the same as size(Y,DIM)).
%
% Example:
% Y = [0 1 2; 3 4 5]
% trapz(Y,1)
% trapz(Y,2)
%
% Class support for inputs X, Y:
% float: double, single
%
% See also SUM, CUMSUM, CUMTRAPZ, INTEGRAL.
% Copyright 1984-2022 The MathWorks, Inc.
% Make sure x and y are column vectors, or y is a matrix.
perm = [];
nshifts = 0;
if nargin == 3 % trapz(x,y,dim)
dim = min(ndims(y)+1, getDimArg(dim));
perm = [dim:max(ndims(y),dim) 1:dim-1];
y = permute(y,perm);
m = size(y,1);
elseif nargin == 2 && isscalar(y) % trapz(y,dim)
dim = y;
y = x;
x = 1;
dim = min(ndims(y)+1, getDimArg(dim));
perm = [dim:max(ndims(y),dim) 1:dim-1];
y = permute(y,perm);
m = size(y,1);
else % trapz(y) or trapz(x,y)
if nargin < 2
y = x;
x = 1;
end
[y,nshifts] = shiftdim(y);
m = size(y,1);
end
if ~isvector(x)
error(message('MATLAB:trapz:xNotVector'));
end
x = x(:);
if ~isscalar(x) && length(x) ~= m
error(message('MATLAB:trapz:LengthXmismatchY'));
end
% The output size for [] is a special case when DIM is not given.
if isempty(perm) && isequal(y,[])
z = zeros(1, 'like', y);
return;
end
% Trapezoid sum computed with vector-matrix multiply.
if isscalar(x)
z = x * sum((y(1:end-1,:) + y(2:end,:)), 1)/2;
else
z = diff(x,1,1).' * (y(1:end-1,:) + y(2:end,:))/2;
end
siz = size(y);
siz(1) = 1;
z = reshape(z,[ones(1,nshifts),siz]);
if ~isempty(perm) && ~isscalar(z)
z = ipermute(z,perm);
end
end
% local function
function dim = getDimArg(dim)
if isnumeric(dim) || islogical(dim)
dim = cast(dim, 'like', 1);
end
dim = matlab.internal.math.getdimarg(dim);
end
0 Comments
John D'Errico
on 7 Dec 2022
Edited: John D'Errico
on 7 Dec 2022
@Bora Eryilmaz gives the code. Thanks for having done that.
In the long term though, this points out why you should strenuously avoid ever using edit on MATLAB supplied code. Just don't edit MathWorks supplied code. It is simply too dangerous, since if accidentally you introduce an extra character someplace and then accidentally save the code, you can create bugs that will be difficult to find. And even though you can recover the code in some way, once you realize what happened, you will be wasting a fair amount of your own time in that effort.
The simple solution is to use type. Type will dump the contents of an m-file into your Command window. Since that gives you the ability to read the code, without ever going into the editor, it is a perfect solution.
type trapz
If you HAVE edited your code, then a second simple solution is to use backups. In fact, just a couple of days ago, I managed to blow away a piece of code of my own. I went into my backup utility, and restored that specific m-file. Problem solved.
Finally, when all else fails, you can just redownload MATLAB. In my case, whenever I download a new MATLAB relase, I use a tool I wrote a few years ago, to export my current MATLAB search path, so that I can then import the search path in the new release. Of course this applies even if I need to re-download a current release.
You can find export_search_path on the file exchange.
Best of course, is to just not touch those supplied m-files.
1 Comment
Steven Lord
on 7 Dec 2022
For long files you may want to turn more on before displaying the file, or you may want to display it in "chunks" (with line numbers) using dbtype.
dbtype why.m 1:5
If you try to ask for a value past the end of the function it will display up through the end, so you could use a very large value as the upper limit if you want to display all of the file past a certain point.
dbtype why.m 282:99999
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!