Code housekeeping: cmmds 'which' and 'cd'

9 views (last 30 days)
Alexander Kazerooni
Alexander Kazerooni on 16 Apr 2012
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??

Answers (3)

Sean de Wolski
Sean de Wolski on 16 Apr 2012
  1. I would guess which isn't showing them because they're not on the MATLAB path at that time.
  2. 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
Alexander Kazerooni
Alexander Kazerooni on 16 Apr 2012
re: on matlab path, I've implemented a funcdtion that adds the path, addpath \\my\dir\path for all my subdirectories that contain implemented code.
Once a dir path is added via add path, should it appear and work with which myfile.m ??
Image Analyst
Image Analyst on 16 Apr 2012
You can do this:
addpath(genpath(yourFolderWithSubfolders));
savepath;

Sign in to comment.


Image Analyst
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.

Walter Roberson
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')

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!