how to use exist for a function that begins with an@

Following @JanSimon's suggestion in this thread. I've defined a function name that starts with an @. But I'm running third-party code that uses `exist` to check the file-type. Unfortunately, `exist` throws an error in this case. To see the problem type
exist(@humps)
Is there a workaround for this?

4 Comments

" I've defined a function name that starts with an @"
The @ symbol does not define a function name, it defines a function handle. The function help clearly states that "Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores." An @ symbol is not part of the name.
"`exist` throws an error in this case"
According to the exist documentation its first input can be the name of a variable, and it confirms the existence of that variable in the workspace. exist does not accept the variable itself as you are attempting.
Because the function handle must exist (using @ creates it) what are you trying to achieve testing for existence immediately after creating it? It exists because you just made it exist.
Are you trying to test for the existence of the function that you want to associate the handle to? Or the existence of a field in the structure returned by the main function of Jan Simon's answer? What are you trying to test for?
Sorry, I should have been clearer. This is an amended version of my comment, correcting a couple of errors and adding more details. The third-party code that I'm running is using exist as a device for checking whether the function is of type 2 or 6. As you say, it's not an issue of whether or not the function exists. My goal is purely to prevent matlab from crashing when it encounters the expression exist(f) in the code, where f is a function handle pointing to a function econGrowth which is defined as a nested function in my main code. The error that's generated is
The first input to exist must be a string scalar or character vector.
Since my initial post, I stumbled on exist(func2str(f)), which does avoid the crash, but I don't know if this is a viable solution in general. Here's an MWE which illustrates the problem. The original code involves multiple third-party files, too numerous to post here, but the problem is the same. My MWE illustrates that the exist(func2str(f)) call returns the indended output, i.e., 2, but the exist(f) call fails
function mfEconGrowth(varargin);
f = @econGrowth;
exist(func2str(f))
exist(f)
function out = econGrowth
out=1
My concern is that my workaround seems like a kludge and will not work in general, which would be a problem since the modification is to a general purpose utility that's called by hundreds of users.
" My goal is purely to prevent matlab from crashing when it encounters the expression test(f) in the code"
What is test? You have not shown/explained how/where it is defined.
What is f? You have not shown/explained how/where it is defined.
fun2str(f): do you mean func2str, or is this a third-party function or class?
So far it is not clear what you are trying to do. It would help us if you explained what you are trying to do, for example: "I have a function X defined in an Mfile which I am trying to supply to a function Y. However it throws an error, the complete text of which is 'Zzzzz zzz zzzz ... Zzzz zzz'", or whatever that explains your situation.
"The third-party code that I'm running is using exist as a device for checking whether the function is of type 2 or 6."
Sure, but how is this related to your question? Do you have such a file? Or not? How are we supposed to know? Your question only mentions that "I've defined a function name that starts with an @" (which is not possible), but nothing about what kind of function you have: is it the main function of an Mfile, or an anonymous function, or a handle to a local function (implied by your link to Jan Simon's answer)? So we have no idea how all of this hangs together.
Please describe the situation clearly.
My sincere apologies for several typos. I've edited my previous comment and I believe addressed all of your remarks now, and includes an MWE.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

on 17 Feb 2018

Commented:

on 17 Feb 2018

Community Treasure Hunt

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

Start Hunting!