Define multiple variables at the same time in MATLAB?

前端 未结 2 1739
轻奢々
轻奢々 2020-12-30 05:28

I don\'t know if MATLAB can do this, and I want it purely for aesthetics in my code, but can MATLAB create two variables at the same time?

Example

x          


        
相关标签:
2条回答
  • 2020-12-30 05:46

    Use comma-separated lists to get multiple variables in the left hand side of an expression.

    You can use deal() to put multiple assignments one line.

    [x,y] = deal(cell(4,8), cell(4,8));
    

    Call it with a single input and all the outputs get the same value.

    [x,y] = deal( cell(4,8) );
    
    >> [a,b,c] = deal( 42 )
    a =
        42
    b =
        42
    c =
        42
    
    0 讨论(0)
  • 2020-12-30 05:49

    It depends on the function that you use to generate the data. You can create your own function in MATLAB that has more than one output:

    [a, b, c] = foo();
    

    Many builtin function also have this option. But this must be supported directly by the returning function.

    0 讨论(0)
提交回复
热议问题