I keep getting two answers for my code
5 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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 件のコメント
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
2020 年 5 月 23 日
Add a semicolon in the last line. And call the function with an output argument.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!