MATLAB Answers

path of a mfile not on search path

9 ビュー (過去 30 日間)
Bruno Luong
Bruno Luong 2019 年 9 月 30 日
コメント済み: Bruno Luong 2019 年 10 月 9 日
When an mfile is not on the search path or current path and when user types the name, MATLAB find it and issues a suggestion
>> foo
'foo' is not found in the current folder or on the MATLAB path, but exists in:
E:\matlab\test
Change the MATLAB current folder or add its folder to the MATLAB path.
Is there anyway to retreive the folder 'E:\matlab\test' programmatically?

  2 件のコメント

Rik
Rik 2019 年 10 月 9 日
I haven't tested this, but if I recall correctly the only folders that are searched are the folders in the drop down menu containing previous current working directory. There may be a way to retrieve that directory list and generate this suggestion yourself. You will probably need some Yair Altman level tricks.
Stephen Cobeldick
Stephen Cobeldick 2019 年 10 月 9 日
" There may be a way to retrieve that directory list..:"
Stored in prefdir, that shouldn't be too hard.

サインイン to comment.

採用された回答

Rik
Rik 2019 年 10 月 9 日
As suggested in a comment here you can use the diary function to capture output to the command window.

  3 件のコメント

Bruno Luong
Bruno Luong 2019 年 10 月 9 日
It doesn't work
function bar
try
foo() % function foo.m NOT in search path
catch
diary('diarytmp.txt');
evalin('base','foo()'); % invoke foo in base workspace
diary('off');
end
end
Then I call
bar()
diarytmp.txt is empty.
EDIT:
dairytmp.txt is flushed with good information (path), when I type
diary off
on command window. Use evalin('base','diary off'); within bar doesn't flush the diary file !
This is weird and too odd and too much contorsion for me to adopt the solution.
Still Rik suggestion of using DIARY answers my question. Better solution is still welcome.
Rik
Rik 2019 年 10 月 9 日
But you do see the text? (I'm on mobile so I can't currently check myself)
Bruno Luong
Bruno Luong 2019 年 10 月 9 日
Yes I see the eror message with the path, but I cannot use it since some one has to type
diary off on command window.
(My purpose is to retrieve this path and suggest in the diaglog box to user to confirm if he/she wants add the path to run FOO)

サインイン to comment.

その他の回答 (1 件)

Jyotsna Talluri
Jyotsna Talluri 2019 年 10 月 9 日
You can get the folders using 'addpath' function

  1 件のコメント

Bruno Luong
Bruno Luong 2019 年 10 月 9 日
My question is how to retreive the PATH name in the fix suggestion provided by MATLAB programatically.
I try try/catch lasterror, lasterr, etc... nothing seems to work. This error message seems to appear only when the mfile is invoked from the base workspace.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by