フィルターのクリア

I can not run my function file for zohplot

1 回表示 (過去 30 日間)
Jialu Yao
Jialu Yao 2023 年 9 月 24 日
編集済み: Walter Roberson 2023 年 9 月 26 日
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 Center および File ExchangeWorkspace 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