Invalid syntax for calling function 'cond' on the path

22 views (last 30 days)
Hi,
I want to extract data from a table called cond. As you can see from line 75 in the screenshot shown below, data Diameter corresponding to Drake can be successfully extracted using cond('Drake',:).Diameter.
However, when I was trying to write this into a function called findCF(), things went wrong at line 78 with an error message
Invalid syntax for calling function 'cond' on the path. Use a valid syntax or explicitly initialize 'cond' to make it a variable.
Can anybody tell me how to modify my code?
Thanks,
Armature

Accepted Answer

dpb
dpb on 19 Nov 2021
>> which -all cond
C:\ML_R2020b\toolbox\matlab\matfun\cond.m
>>
cond is a builtin MATLAB function (the condition number) unless you alias it as a variable as you have done in the earlier code.
But, inside the function findCF the table isn't defined as you didn't pass the variable cond to it nor even make provision to have it in the argument list so it isn't available.
All functions have their own workspace; even though you have defined a variable in the calling workspace it isn't visible inside a called function unless passed (or made global, but that's not a good solution).
To avoid using MATLAB functions as variable is one reason I use a prepended 't' on table variables so I would have named your table as tCond and the function definition would look like
function CF=findCF(tCond,Name,n,D,d)
and you would call it passing tCond as the first argument.
NB: The variable name in the caller and the callled routine are not required to be the same; they are associated by position in the argument list only, not by name; I only used the same one here out of being the fewest code changes needed to avoid aliasing and to pass the table.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!