Using dir() without changing directory?

52 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 10 月 11 日
編集済み: Stephen23 2023 年 4 月 24 日
I need to find files that have dir(['*K1*.json']); in their names which are in a specific folder different from the matlab script folder. I prefere not to change directory because I will get error while running the script.
Is there any way to do so?
  6 件のコメント
Jim Riggs
Jim Riggs 2019 年 10 月 11 日
Perhaps you did not understand:
The path specification "/users/geoffh/someFolder/" represents the full path to the folder that contains the files that you want to search. You need to substitude the actual path to your files.
If you specify a path in the 'dir' command, it will searth that specified folder.
Stephen23
Stephen23 2019 年 10 月 12 日
編集済み: Stephen23 2023 年 4 月 24 日
"I prefere not to change directory because I will get error while running the script. "
Changing folders to access data files is slow and makes the code harder to debug. Best avoided.
It is much better to use absolute/relative filenames, as the others have already commented.
"the folder containing *K1*.json is differnt from matlab running script... I need a structure that can define the folder containing K1.json files."
That is exactly what absolute/relative filenames are for:

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

採用された回答

per isakson
per isakson 2019 年 10 月 12 日
Use an absolute path rather than a relative path (as proposed in the comments). It's easier to get it right. Thus try
sad = dir( fullfile( 'c:', 'specific', 'folder', ['*K1*.json'] ) );

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by