How do I put variable values into a text string in MATLAB?

前端 未结 5 1172
终归单人心
终归单人心 2021-02-13 19:17

I\'m trying to write a simple function that takes two inputs, x and y, and passes these to three other simple functions that add, multiply, and divide

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-13 20:11

    I just realized why I was having so much trouble - in MATLAB you can't store strings of different lengths as an array using square brackets. Using square brackets concatenates strings of varying lengths into a single character array.

        >> a=['matlab','is','fun']
    
    a =
    
    matlabisfun
    
    >> size(a)
    
    ans =
    
         1    11
    

    In a character array, each character in a string counts as one element, which explains why the size of a is 1X11.

    To store strings of varying lengths as elements of an array, you need to use curly braces to save as a cell array. In cell arrays, each string is treated as a separate element, regardless of length.

    >> a={'matlab','is','fun'}
    
    a = 
    
        'matlab'    'is'    'fun'
    
    >> size(a)
    
    ans =
    
         1     3
    

提交回复
热议问题