Adrian Hartanto
Adrian Hartanto

Reputation: 43

MATLAB - function input argument is undefined?

I have code like this:

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

That called a function like this one:

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

Error messages:

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);

I've also read the Stack Overflow question Input argument undefined - MATLAB function/subfunction, but still I have no clue.

Upvotes: 1

Views: 3808

Answers (1)

dantswain
dantswain

Reputation: 5467

Is this what you mean to do?

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

Calling functions with multiple output arguments

Suppose you write a function like the following:

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

You would then call that function like this:

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

I think one of your confusions is with the naming. It's maybe less confusing if I call it like this:

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

Upvotes: 1

Related Questions