Code housekeeping: cmmds 'which' and 'cd'
9 views (last 30 days)
Show older comments
Hello,
I am cleaning up project code and discovered the which command does not work with my own codfiles(scripts or functions) that are .m, i.e. which mycodefile.m >> 'mycodefile.m' not found;
Also, when I say input_dir='\\my\dir\path' or indput_dir= 'h:\matlab'; then cd (input_dir), it doesn not work, but when entering cd(\\my\dir\path\) or cd ('\\my\dir\path\'), it does not work??
1 Comment
Answers (3)
Sean de Wolski
on 16 Apr 2012
- I would guess which isn't showing them because they're not on the MATLAB path at that time.
- Which syntax is working?
input_dir='\\my\dir\path'
cd(input_dir) %calling with stringvariable
should work the same way as:
cd('\\my\dir\path') %string
Calling cd() with that as a variable, i.e.
cd(\\my\dir\path)
I would expect to error.
4 Comments
Image Analyst
on 16 Apr 2012
I almost never use cd. I always leave the current folder as the current working directory of the m-file. If I have another folder, for example where data files live, I just keep track of that folder in a variable and then use fullfile() to build the full file name of the file I need to access. I think it's risky to use cd in any but the simplest script. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.
If you really insist on hard coding paths in, just avoid the whole backslash ambiguity altogether and use forward slashes. Forward slashes work with both Unix and Windows. No need to use backslashes at all in Windows MATLAB.
0 Comments
Walter Roberson
on 16 Apr 2012
cd(\\my\dir\path)
is simply invalid syntax.
cd '\\my\dir\path'
takes advantage of command / function duality. That says that if you give the name of a function, and follow it by space-separated values, then each of the values you specify will be passed to the function as a string. For example, if you had a command froboz then
froboz gnu yellow quickly
would be equivalent to froboz('gnu','yellow','quickly')
That's why cd '\\my\dir\path' works. But when you invoke
cd(\\my\dir\path)
then you are already giving function form, so the part inside () has to be a valid MATLAB expression such as a quoted string.
cd('\\my\dir\path')
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!