MATLAB - 函数输入参数未定义?

时间:2011-12-09 00:44:08

标签: function matlab

我有这样的代码:

JSenin1 = [1 0 1 3 ;1 0 1 3 ;1 0 1 3 ;0 0 0 0 ;0 0 0 0 ;1 1 0 4 ;1 1 0 4 ;1 1 0 4 ;1 1 0 4 ;0 0 0 0];
JSenin2 = [1 0 0 3 ;1 0 0 3 ;1 1 1 3 ;1 1 1 2 ;1 1 1 2 ;0 0 0 0 ;1 1 0 4 ;1 1 0 4 ;1 1 0 4 ;1 1 0 4];
JSenin3 = [1 1 1 3 ;1 1 1 3 ;1 1 1 3 ;0 0 0 0 ;0 0 0 0 ;0 0 0 0 ;1 0 0 4 ;1 0 0 4 ;1 0 0 4 ;1 0 0 4];
JSenin4 = [1 0 0 3 ;1 0 0 3 ;1 0 0 3 ;0 0 0 0 ;1 0 1 2 ;1 0 1 2 ;0 0 0 0 ;1 1 0 3 ;1 1 0 3 ;1 1 0 3];

i = 1;
jadwal = 0;
while i < 11
    a = eq(1, JSenin1(i, 1));
    b = eq(1, JSenin2(i, 1));
    c = eq(1, JSenin3(i, 1));
    d = eq(1, JSenin4(i, 1));

    if a == 1
        fungsi1(JSenin1, JSenin2, JSenin3, JSenin4, i)
        i = fungsi1(i); %I want to take value "i" back from "fungsi1".

    elseif b == 1

    elseif c == 1

    elseif d == 1

    end
    i = i + 1;
end

调用了这样一个函数:

function [ jadwal,i ] = fungsi1( JSenin1,JSenin2,JSenin3,JSenin4,i )

    %UNTITLED Summary of this function goes here.
    %   Detailed explanation goes here.

    a = eq(JSenin1(i,1),JSenin2(i,1));
    b = eq(JSenin1(i,1),JSenin3(i,1));
    c = eq(JSenin1(i,1),JSenin4(i,1));
    if a == 1 && b == 1 && c == 1
        d = eq(JSenin1(1,4),JSenin2(1,4));
        e = eq(JSenin1(1,4),JSenin3(1,4));
        f = eq(JSenin1(1,4),JSenin4(1,4));
        if d == 1 && e == 1 && f == 1
            jadwal = ([JSenin1(i,2:3);JSenin2(i,2:3);JSenin3(i,2:3);JSenin4(i,2:3)]);
            i = i + JSenin1(i,4) - 1; %I need to take this variable,
                                      %but I got myself an error.
        elseif d == 1 && e == 1 && f == 0
        elseif d == 1 && e == 0 && f == 1
        elseif d == 0 && e == 1 && f == 1
        end
    end
end

错误讯息:

ans =

     0     1
     0     0
     1     1
     0     0

??? Input argument "i" is undefined.

Error in ==> fungsi1 at 4
a = eq(JSenin1(i,1),JSenin2(i,1));

Error in ==> Tes at 16
        i = fungsi1(i);

我还阅读了Stack Overflow问题 Input argument undefined - MATLAB function/subfunction ,但我仍然不知道。

1 个答案:

答案 0 :(得分:1)

这是你的意思吗?

[jadwal, i] = fungsi1(JSenin1,JSenin2,JSenin3,JSenin4,i)

使用多个输出参数调用函数

假设您编写了如下函数:

function [a_squared, a_cubed] = square_and_cube(a)
a_squared = a^2;
a_cubed = a^3;

然后您可以像这样调用该函数:

a = 2;
[a_squared, a_cubed] = square_and_cube(a);
disp(a_squared) % -> 4
disp(a_cubed)   % -> 8

我认为你的一个困惑在于命名。如果我这样称呼它可能不那么令人困惑:

x = 2;
[x_squared, x_cubed] = square_and_cube(x);