I keep getting two answers for my code
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
ahm tay
el 23 de Mayo de 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
0 comentarios
Respuesta aceptada
Stephen23
el 23 de Mayo de 2020
Editada: Stephen23
el 23 de Mayo de 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:
2 comentarios
Stephen23
el 23 de Mayo de 2020
Editada: Stephen23
el 24 de Mayo de 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.
Más respuestas (1)
madhan ravi
el 23 de Mayo de 2020
Add a semicolon in the last line. And call the function with an output argument.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!