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

前端 未结 5 1171
终归单人心
终归单人心 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:13

    As Peter and Amro illustrate, you have to convert numeric values to formatted strings first in order to display them or concatenate them with other character strings. You can do this using the functions FPRINTF, SPRINTF, NUM2STR, and INT2STR.


    With respect to getting ans = 3 as an output, it is probably because you are not assigning the output from answer to a variable. If you want to get all of the output values, you will have to call answer in the following way:

    [out1,out2,out3] = answer(1,2);
    

    This will place the value d in out1, the value e in out2, and the value f in out3. When you do the following:

    answer(1,2)
    

    MATLAB will automatically assign the first output d (which has the value 3 in this case) to the default workspace variable ans.


    With respect to suggesting a good resource for learning MATLAB, you shouldn't underestimate the value of the MATLAB documentation. I've learned most of what I know on my own using it. You can access it online, or within your copy of MATLAB using the functions DOC, HELP, or HELPWIN.

提交回复
热议问题