I keep getting two answers for my code

조회 수: 5 (최근 30일)
ahm tay
ahm tay 2020년 5월 23일
편집: Stephen23 2020년 5월 24일
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

채택된 답변

Stephen23
Stephen23 2020년 5월 23일
편집: Stephen23 2020년 5월 23일
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
ahm tay
ahm tay 2020년 5월 23일
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.
Stephen23
Stephen23 2020년 5월 23일
편집: Stephen23 2020년 5월 24일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 5월 23일
Add a semicolon in the last line. And call the function with an output argument.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by