unable to find function / Previously accessible file
20 views (last 30 days)
Show older comments
Hi, I started receiving an error while executing a long script, where I:
- write a cell of anonumous functions with frintpf into a 'modele.m' file (a lot of functions created in a loop), fclose the file
- run it with run('modele.m'), a cell of functions I needed is created into the workspace
- Proceed with some other calculations
- Perform a lsqcurvefit using previously defined cell of functions. The error shows up.
It says:
Unable to find function @(beta,t)(stalap(11293952,beta(1),beta(3),beta(4),beta(2),beta(5),beta(6),beta(7),beta(8),beta(33))*exp(-Rp(11293952,beta(1),beta(3),beta(4),beta(2),beta(5),beta(6))*t)+stalam(11293952,beta(1),beta(3),beta(4),beta(2),beta(5),beta(6),beta(7),beta(8),beta(33))*exp(-Rm(11293952,beta(1),beta(3),beta(4),beta(2),beta(5),beta(6))*t)+beta(7)*11293952+beta(8)).^0.5 within C:\Users\E09\OneDrive\program\arabic\modele.m.
I don't understand why matlab tries to access the file I used lines of code before during the fit. If I paste the definition of my functions explicitly into the script and delete all the references to the 'modele.m' file, the error changes to:
Previously accessible file "C:\Users\E09\OneDrive\program\arabic\modele.m" is now inaccessible.
What is even weirder - the script works just fine on another computer.
What may be the reason of such behaviour?
0 Comments
Answers (2)
Athul Prakash
on 16 Oct 2020
Hi Malgorzata,
Instead of saving the strings to a .m code file and executing that code, I suggest using eval() to have them populate the workspace. eval() can parse strings as matlab code and run them, so it should be able to populate your workspace with those function handles if you invoke it on your strings.
Then ff you need to save them, you may try writing the function handles to a .mat.
Regarding the error, I think the matlab path has somehow changed.
First, have a look at this documentation to see how the MATLAB search path works. savepath() can be used to prevent files from leaving the Matlab path accidentally.
Hope it helps!
2 Comments
Emily Mankin
on 13 Mar 2023
I have a similar problem, but none of the issues suggested in this solution as the root of the problem. Specificaly, I don't write the functions to a text file and read them later.
I define several anonymous function handles within a cell array in my code, each of which should return a true/false answer.
Later I use cellfun to determine which of these handles returns true.
For example, inside fuctionFileName.m, I might write something like
criteriaTests = {@(x)(x+2)>4, @(x)(x-7)<5, @(x)x==3};
then later have a line
whatIsTrue = cellfun(@(y)y(7),criteriaTests);
This works most of the time. But sometimes I instead get an error that says
Unable to find function @(x)(x+2)>4 within fuctionFileName.m
At first I thought it was an issue with Matlab version, as I had been using an older version of Matlab and upgraded to 2022a. But I switched back to 2016a, and it worked for a while then suddenly stopped working.
I would really love a solution, as I wrote a lot of code expecting this functionality to work, and my code is all crashing now. (Though like the OP, it seems computer/moment dependent)
Emily Mankin
on 13 Mar 2023
Well, after trying all manner of solution, including adding eval or feval into my code, and resetting the Matlab path (none of which helped), I finally tried restarting Matlab. Which has done the trick, at least for now. It's not a good solution, as I have a lot of startup "cost" in terms of loading time for my data before I get to where I can be working on my code development. But at least it's clear that the code really can work without extra modifications.
2 Comments
Steven Lord
on 13 Mar 2023
Are you perhaps trying to write the file with your dynamic code to a directory inside matlabroot? If so move the file outside of the MATLAB installation folder structure.
Or is your file being written on a network directory? If there's latency between your machine and that of the network directory, MATLAB may not "recognize" the new file if you try to call it immediately after you create it.
Emily Mankin
on 15 Mar 2023
Edited: Emily Mankin
on 15 Mar 2023
Thanks for looking at my issue...
There's no separate file, though, in my case.
I'm working with objects, and inside my code that initializes the objects, I have lines of code that define my cell array of function handles. e.g.
obj.myFunctions = {@(x)x>2, @(x)x<7};
I'm using a method defined in the same file and am usually able to call these function handles without issue. But occasionally I get the error that the function isn't found. But it's such a strange error, because it writes out the function as an anonymous function in the error (e.g. Unable to find function @(x)x>2), so it really seems that Matlab has access to the function definition. It shouldn't be a path issue, since it's an anonymous function it knows about.
Anyway, I'm perplexed by the error but I found a solution. Rerunning the line from the initialization code fixes the error. So it seems like somehow that field of the object is becoming inaccessible? Getting corrupted?
I moved the function handle definitions to their own method, so now I run that before I do any operations using obj.myFunctions and I haven't had the problem again.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!