Code housekeeping: cmmds 'which' and 'cd'

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

Walter Roberson
Walter Roberson 2012년 4월 16일
Sorry, which form of cd works and which does not?

댓글을 달려면 로그인하십시오.

답변 (3개)

Sean de Wolski
Sean de Wolski 2012년 4월 16일

0 개 추천

  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

Image Analyst
Image Analyst 2012년 4월 16일
Or 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.
Alexander Kazerooni
Alexander Kazerooni 2012년 4월 16일
>> cd '\\my\dir\path'
>> pwd
ans =\\my\dir\path
>> cd(\\my\dir\path)
??? cd(\\my\dir\path)
|
Error: Unexpected MATLAB operator.
>> cd('\\my\dir\path)
>> pwd
ans =
\\my\dir\path
Alexander Kazerooni
Alexander Kazerooni 2012년 4월 16일
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 2012년 4월 16일
You can do this:
addpath(genpath(yourFolderWithSubfolders));
savepath;

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 4월 16일

0 개 추천

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 2012년 4월 16일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

제품

태그

질문:

2012년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by