我是matlab的新手我正在尝试定义一个函数并且我一直收到此错误
“输入参数”ip“未定义。
==>中的错误edge_mapping为2 size_ip = size(ip(:,:,1)); “
这是我的代码
function[op1,op2,op3] = edge_mapping(ip)
size_ip = size(ip(:,:,1));
s=size_ip(1);
op1= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op2= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
op3= cat(3,zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s),zeros(s));
for i = 1 : 10
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
答案 0 :(得分:1)
function [op1, op2, op3] = edge_mapping(ip)
op1 = zeros(size(ip));
op2 = zeros(size(ip));
op3 = zeros(size(ip));
for i = 1 : size(ip, 3)
op1(:,:,i)=edge(ip(:,:,i),'sobel');
op2(:,:,i)=edge(ip(:,:,i),'prewitt');
op3(:,:,i)=edge(ip(:,:,i),'canny');
end
return
但我认为写一个简单的函数更好:
function op = edge_mapping(ip, edge_mode)
op = zeros(size(ip));
for i = 1 : size(ip, 3)
op(:,:,i)=edge(ip(:,:,i), edge_mode);
end
return
然后调用它:
op_sobel = edge_mapping(ip, 'sobel');
op_prewitt = edge_mapping(ip, 'prewitt');
op_canny = edge_mapping(ip, 'canny');
答案 1 :(得分:0)
此代码是一个功能。它必须保存为m文件并从MATLAB命令行,脚本或其他函数运行
[op1,op2,op3] = edge_mapping(ip);
其中参数ip,op1,op2和op3可以有不同的名称。
确保function
关键字后面有空格。
要避免此错误,您可以为输入参数指定默认值(如果未定义)(函数范围内不存在):
if ~exist(ip, 'var')
ip = []; %# or other default value
end