MATLAB -多重返回值从一个函数?

时间:2023-02-03 02:02:32

I'm writing 2 functions in matlab, an initialize function and a function to insert items into an array treating it like a doubly-linked list. However, my initialize function only returns "ans =" and the initialized array. How can I have it also set values of my other variables? Here's my code:

我在matlab中编写了2个函数,一个初始化函数和一个函数将项插入到数组中,把它当作一个双链表。然而,初始化函数只返回“ans =”和初始化数组。我怎么能让它同时设置其他变量的值呢?这是我的代码:

function [ array, listp, freep ] = initialize( size )
    array = zeros(size, 3);
    listp = 0;
    freep = 1;
end

4 个解决方案

#1


78  

Matlab allows you to return multiple values as well as receive them inline.

Matlab允许您返回多个值,同时还可以内联接收它们。

When you call it, receive individual variables inline:

当你调用它时,内联接收单个变量:

[array, listp, freep] = initialize(size)

#2


0  

I think Octave only return one value which is the first return value, in your case, 'array'.

我认为Octave只返回一个值,它是第一个返回值,在你的情况下,'array'。

And Octave print it as "ans".

Octave把它打印成“ans”。

Others, 'listp','freep' were not printed.

其他人,“listp”,“freep”没有打印出来。

Because it showed up within the function.

因为它出现在函数内。

Try this out:

试试这个:

[ A, B, C] = initialize( 4 )

And the 'array','listp','freep' will print as A, B and C.

“数组”,“listp”,“freep”将打印为A、B和C。

#3


-1  

Change the function that you get one single Result=[array, listp, freep]. So there is only one result to be displayed

改变你得到一个单一结果的函数=[数组,listp, freep]。所以只显示一个结果。

#4


-1  

Use the following in the function you will call and it will work just fine.

在你将要调用的函数中使用下面的函数,它会运行得很好。

     [a b c] = yourfunction(optional)
     %your code
     a = 5;
     b = 7;
     c = 10;
     return
     end

This is a way to call the function both from another function and from the command terminal

这是一种从另一个函数和命令终端调用函数的方法。

     [aa bb cc] = yourfunction(optional);

The variables aa, bb and cc now hold the return variables.

变量aa、bb和cc现在保留返回变量。

#1


78  

Matlab allows you to return multiple values as well as receive them inline.

Matlab允许您返回多个值,同时还可以内联接收它们。

When you call it, receive individual variables inline:

当你调用它时,内联接收单个变量:

[array, listp, freep] = initialize(size)

#2


0  

I think Octave only return one value which is the first return value, in your case, 'array'.

我认为Octave只返回一个值,它是第一个返回值,在你的情况下,'array'。

And Octave print it as "ans".

Octave把它打印成“ans”。

Others, 'listp','freep' were not printed.

其他人,“listp”,“freep”没有打印出来。

Because it showed up within the function.

因为它出现在函数内。

Try this out:

试试这个:

[ A, B, C] = initialize( 4 )

And the 'array','listp','freep' will print as A, B and C.

“数组”,“listp”,“freep”将打印为A、B和C。

#3


-1  

Change the function that you get one single Result=[array, listp, freep]. So there is only one result to be displayed

改变你得到一个单一结果的函数=[数组,listp, freep]。所以只显示一个结果。

#4


-1  

Use the following in the function you will call and it will work just fine.

在你将要调用的函数中使用下面的函数,它会运行得很好。

     [a b c] = yourfunction(optional)
     %your code
     a = 5;
     b = 7;
     c = 10;
     return
     end

This is a way to call the function both from another function and from the command terminal

这是一种从另一个函数和命令终端调用函数的方法。

     [aa bb cc] = yourfunction(optional);

The variables aa, bb and cc now hold the return variables.

变量aa、bb和cc现在保留返回变量。