How can I use a variable (i.e. a folder path) as the argument for cd function?

Hi! I think this is a syntax-type question:
I want to use cd to change between directories that are stored in a looping variable. What would be the syntax so cd('path'); can be performed, 'path' being :\Users\p_jarami\A\'

 採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 7 日
projectdir = 'C:\Users\p_jarami\A\';
cd(projectdir)
You should avoid using a variable named "path" as that variable means special things to MATLAB.

5 件のコメント

Pablo Jaramillo Restrepo
Pablo Jaramillo Restrepo 2016 年 2 月 7 日
Thanks for your prompt response Walter,
I think I should've been more specific. I am not actually using 'path as a variable'. I am looking for a way to use this variable: stationFolder_path2 which is a char-type variable ( 'C:\Users\p_jarami\Desktop\Env. Canada, Montreal - 50km\Daily Data\7011947\'), to get matlab to access this directory using cd (or perhaps another function could work).
The reason of having the path as a variable is because I have this in a For loop that will access multiple folders (and do some stuff in there, then return to the bigger folder and access the next sub-folder), so each time stationFolder_path2 is going to be different. For example the next path will be ' C:\Users\p_jarami\Desktop\Env. Canada, Montreal - 50km\Daily Data\7013279\'
I hope I am more clear now
cd(stationFolder_path2)
Qin Tony
Qin Tony 2018 年 6 月 14 日
編集済み: Qin Tony 2018 年 6 月 14 日
Hi Walter
Similar issue occurred for my case. I need to define refreshed path in one loop. Variable as A=c:\example....; A is changing for each loop. I want to apply cd for variable A as 'c:\example....', but neither cd A nor cd 'A' worked... Could you please help me on this?
Adam
Adam 2018 年 6 月 14 日
You could try the solution in the answer you are replying to!! You went to the trouble of finding a relevant question and commenting on an answer so why not simply apply the answer?!
cd( A )
It's highly unlikely that using cd and changing directory in a loop is a good idea in most cases, but that is another matter, depending what you are doing. Using full paths to files is generally far better.
Maria Byrne
Maria Byrne 2020 年 7 月 16 日
what worked for me was cd(char(A)) since A was actually a cell even though it looked like a string

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by