unable to find function / Previously accessible file
19 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Athul Prakash
2020-10-16
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 个评论
Emily Mankin
2023-3-13
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
2023-3-13
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 个评论
Steven Lord
2023-3-13
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
2023-3-15
编辑:Emily Mankin
2023-3-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!