# I keep getting two answers for my code

2 views (last 30 days)
ahm tay on 23 May 2020
Edited: Stephen Cobeldick on 24 May 2020
I get my named output fconvg=" " but then I also get ans = " "
I don't understand where the ans = is coming from as I have all the semi colons needed?
function [ fconvg ] = AHMconvolve( f, g )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
syms x t;
k = subs(g,t,t-x);
j = subs(f,t,x);
if has(int(k*j, x, 0, t),'int')
k = subs(f,t,t-x);
j = subs(g,t,x);
else
k = subs(g,t,t-x);
j = subs(f,t,x);
fconvg = int(k*j, x, 0, t)
end

Stephen Cobeldick on 23 May 2020
Edited: Stephen Cobeldick on 23 May 2020
fconvg = int(k*j, x, 0, t)
% ^ missing semi-colon
This line should probably come after the if-end.
The reason that you get
ans = " "
is because you are not assigning the function output to any variable. You need to call your function like this:
fout = AHMconvolve(...)
^^^^^^ you need to assign to an output like this!
Yes I deliberately chose a different name to what it is named inside the function, to emphasisze that you are worknig in a different workspace, and the variable names used inside the function are (and should be) totally irrelevant.
How to call functions with output arguments and assign their outputs to variables is explained in the introductory tutorials:

ahm tay on 23 May 2020
I understand that and have read the intro tutorials, but that only works in the command window so I would have to specify each time fout = AHMconvolve(...) for each different input in AHMconvolve. I want a way to permenantly assign in the code, 'fout' to AHMconvolve for every input argument.
Stephen Cobeldick on 23 May 2020
"but that only works in the command window"
No, it actually works anywhere that you can call your function, i.e. it works in any function, script, and class.
"so I would have to specify each time fout = AHMconvolve(...) for each different input in AHMconvolve"_
That is not how MATLAB works: you just need to use a loop and indexing into vectors/matrices/arrays.
"I want a way to permenantly assign in the code, 'fout' to AHMconvolve for every input argument."
I don't know what you mean by "permenantly assign" because generally nothing in code is "permanent", and the term has no specific meaning in MATLAB. However you can trivially call your function multiple time in a loop and use indexing to assign the output to a variable. That is how MATLAB works. That is what you should do.
How to assign values to a variable using indexing is explained in the introductory tutorials.