I can not run my function file for zohplot

1 次查看(过去 30 天)
function zohplot(xord,yord,plotchar)
% function zohplot(xord,yord,plotchar)
%
% zohplot.m Plots the zero-order hold of two columns of data. If
% yord is a matrix then it plots each column of yord vs
% the vector xord. xord and yord must have the same
% row dimension.
%
% xord = data in the x coordinate
% yord = data in the y coordinate
% plotchar = the line or point-type as in plot(x,y,'plotchar')
%
% Written 2/24/87 by Daniel Abramovitch.
%
% modified last 3/18/87 FRH
%
[xlen,xwid] = size(xord);
xlen = xlen-1;
%
% if xlen==0, then assume data was provided in row rather than
% column format and transpose input. these variables are local,
% so the change will not affect the users format.
%
if xlen==0
xord=xord';
[xlen,xwid] = size(xord);
xlen = xlen-1;
yord=yord';
end
[ylen,ywid] = size(yord);
ylen = ylen-1;
if xlen~=ylen
disp('--ERROR-- xord and yord must have same number of rows')
return
end
bigx=zeros(2*xlen,1); % Fill these with zero;
bigy=zeros(2*ylen,ywid);
txlm1 = 2*xlen-1;
txl = 2*xlen;
xlp1 = xlen+1;
bigx(1:2:txlm1) = xord(1:xlen);
bigx(2:2:txl) = xord(2:xlp1);
bigy(1:2:txlm1,:) = yord(1:xlen,:);
bigy(2:2:txl,:) = yord(1:xlen,:);
if(nargin <= 2)
plot(bigx,bigy);
else
plot(bigx,bigy,plotchar);
end
%
Can u help me debug this code. It shows that unable to run code and shows me Unrecognized function or variable 'xord'. I'm confused about this error.

采纳的回答

Walter Roberson
Walter Roberson 2023-9-25
When you attempt to run a function by pressing the green Run button, MATLAB never, I repeat never searches the calling environment or the base workspace or the global workspace looking for variables that have the same names as the named parameters such as xord . The only way that MATLAB can know what value to use for parameters named in a function definition is if the call to the function passes in values to the parameters positionally.
x = 0:.1:1;
y = rand(size(x));
pc = '-*';
zohplot(x, y, pc)
function zohplot(xord,yord,plotchar)
% function zohplot(xord,yord,plotchar)
%
% zohplot.m Plots the zero-order hold of two columns of data. If
% yord is a matrix then it plots each column of yord vs
% the vector xord. xord and yord must have the same
% row dimension.
%
% xord = data in the x coordinate
% yord = data in the y coordinate
% plotchar = the line or point-type as in plot(x,y,'plotchar')
%
% Written 2/24/87 by Daniel Abramovitch.
%
% modified last 3/18/87 FRH
%
[xlen,xwid] = size(xord);
xlen = xlen-1;
%
% if xlen==0, then assume data was provided in row rather than
% column format and transpose input. these variables are local,
% so the change will not affect the users format.
%
if xlen==0
xord=xord';
[xlen,xwid] = size(xord);
xlen = xlen-1;
yord=yord';
end
[ylen,ywid] = size(yord);
ylen = ylen-1;
if xlen~=ylen
disp('--ERROR-- xord and yord must have same number of rows')
return
end
bigx=zeros(2*xlen,1); % Fill these with zero;
bigy=zeros(2*ylen,ywid);
txlm1 = 2*xlen-1;
txl = 2*xlen;
xlp1 = xlen+1;
bigx(1:2:txlm1) = xord(1:xlen);
bigx(2:2:txl) = xord(2:xlp1);
bigy(1:2:txlm1,:) = yord(1:xlen,:);
bigy(2:2:txl,:) = yord(1:xlen,:);
if(nargin <= 2)
plot(bigx,bigy);
else
plot(bigx,bigy,plotchar);
end
%
end
  2 个评论
Jialu Yao
Jialu Yao 2023-9-25
Hi, Walter. Thanks for answering my question. I know what do you mean for calling the function. However, I cann't even run the function and it shows me this file has an invalid extension and it asks me to change the variable in error message. I don't know why this happen?
Walter Roberson
Walter Roberson 2023-9-25
编辑:Walter Roberson 2023-9-26
You need to use lower-case .m rather than .M for the file extension.
In order to run
ZOHPLOT(xord, yord, plotchar)
at the command prompt, you would need to have assign values to the variables xord yord and plotchar
Note: the function does not care what you name your variables outside the function: it gets values strictly positionally. You could have done something like
x = 0:.1:1;
y = rand(size(x));
pc = '-*';
ZOHPLOT(x, y, pc)
at the command line and it will be fine.
The name you invoke the function under must match the name of the file -- so if you create ZOHPLOT.m then you need to invoke it as ZOHPLOT but if you rename to zohplot.m then you would invoke it as zohplot . The fact that you have function zohplot inside the function will be ignored if you name the filke ZOHPLOT.m and invoke it as ZOHPLOT : for the first function inside a function file, the name of the file overrides the name on the function line.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by