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

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

    I was looking for something along what you wanted, but wanted to put it back into a variable.

    So this is what I did

    variable = ['hello this is x' x ', this is now y' y ', finally this is d:' d]

    basically

    variable = [str1 str2 str3 str4 str5 str6]

    0 讨论(0)
  • 2021-02-13 20:10

    Here's how you convert numbers to strings, and join strings to other things (it's weird):

    >> ['the number is ' num2str(15) '.']
    ans =
    the number is 15.
    
    0 讨论(0)
  • 2021-02-13 20:10

    You can use fprintf/sprintf with familiar C syntax. Maybe something like:

    fprintf('x = %d, y = %d \n x+y=%d \n x*y=%d \n x/y=%f\n', x,y,d,e,f)
    

    reading your comment, this is how you use your functions from the main program:

    x = 2;
    y = 2;
    [d e f] = answer(x,y);
    fprintf('%d + %d = %d\n', x,y,d)
    fprintf('%d * %d = %d\n', x,y,e)
    fprintf('%d / %d = %f\n', x,y,f)
    

    Also for the answer() function, you can assign the output values to a vector instead of three distinct variables:

    function result=answer(x,y)
    result(1)=addxy(x,y);
    result(2)=mxy(x,y);
    result(3)=dxy(x,y);
    

    and call it simply as:

    out = answer(x,y);
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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.

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