views:

853

answers:

4

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 them. The main function should then display the results as a string containing x, y, and the totals.

I think there's something I'm not understanding about output arguments. Anyway, here's my (pitiful) code:

function a=addxy(x,y)
a=x+y;

function b=mxy(x,y)
b=x*y;

function c=dxy(x,y)
c=x/y;

The main function is:

function [d e f]=answer(x,y)
d=addxy(x,y);
e=mxy(x,y);
f=dxy(x,y);
z=[d e f]

How do I get the values for x, y, d, e, and f into a string? I tried different matrices and stuff like:

['the sum of' x 'and' y 'is' d]

but none of the variables are showing up.

Two additional issues:

  • Why is the function returning "ans 3" even though I didn't ask for the length of z?
  • If anyone could recommend a good book for beginners to MATLAB scripting I'd really appreciate it.
+1  A: 

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.
Peter
Thank you very much.
jefflovejapan
That was helpful, but it's still not working the way I want it to. When I run the function on (1,2) I get ans=3 , which means that the other two functions, mxy and dxy, aren't seeing the values of x and y. Also, the values for all of the output arguments are empty.I think there's something really simple that I'm not doing here with the variables, but I can't figure out what it is.
jefflovejapan
+5  A: 

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);
Amro
thanks, this is definitely simpler, but the exercise specifically asks you to build functions and to have them pass variables back and forth.
jefflovejapan
This is great, thanks a lot. It's cool that you don't have to specify the size of 'result' beforehand.
jefflovejapan
+3  A: 

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.

gnovice
Thanks, this is very useful. Just one more quick question - if I create an output variable in one function - for example:function a=simple(x);a=x^2;how can i access the value of 'a' from other functions? For example:function b=simple2(x);b=a*x; %calling on a from 'simple'
jefflovejapan
@jefflovejapan: In such a case, your second function would be `function b = simple2(x) b = simple(x)*x;`. In other words, you would call `simple` within `simple2` and its output will be used in the equation for `b`.
gnovice
A: 

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
jefflovejapan