1

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.

Community
  • 1
  • 1
Adrian Hartanto
  • 43
  • 1
  • 2
  • 6

1 Answers1

1

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);
dantswain
  • 5,427
  • 1
  • 29
  • 37
  • no, sorry what i want to do, is passing back the value i from fungsi1 to the main code.. but i dnt know how to do it the [jadwal, i] = fungsi1(JSenin1,JSenin2,JSenin3,JSenin4,i) has nothing to do, it's just my experiment... tq for answering – Adrian Hartanto Dec 09 '11 at 01:23
  • No, that's exactly what this code does. It's different than what you wrote. Just try it. – dantswain Dec 09 '11 at 01:28
  • so, what must wrote? i dnt get any idea? to just call back the value from a fuction i just need to write i = fungsi1(i)? – Adrian Hartanto Dec 09 '11 at 01:29
  • No. Replace the two lines after "if a == 1" with [jadwal, i] = fungsi1(JSenin1,JSenin2,JSenin3,JSenin4,i). Just do it. I will try to edit my answer to clarify. – dantswain Dec 09 '11 at 01:41
  • still error sir, the line that i need to change is this i = fungsi1(i); with [jadwal, i] = fungsi1(JSenin1,JSenin2,JSenin3,JSenin4,i) – Adrian Hartanto Dec 09 '11 at 02:12
  • it produces an error ??? Output argument "jadwal" (and maybe others) not assigned during call to "D:\fungsi1.m>fungsi1". Error in ==> Tes at 16 [jadwal, i] = fungsi1(JSenin1,JSenin2,JSenin3,JSenin4,i); – Adrian Hartanto Dec 09 '11 at 02:13
  • That's a different problem. Look at your code. You only assign jadwal and i if d == 1 && e == 1 && f == 1. You can get output if you just put jadwal = 0 at the top of the function. Chances are it's not the output you want, but you should solve your problems one at a time. – dantswain Dec 09 '11 at 02:41
  • If this solved your problem, you should click the check mark button :) – dantswain Dec 09 '11 at 03:15