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

Geoff Hayes
Geoff Hayes 2019년 10월 11일
Zeynab - rather than changing directories, why not just include the full path to that folder that has those files that you are looking for? Use that full path when calling dir.
@Geoff Hayes what do you mean by full path?
The full path to your folder could be something like
dir(['/users/geoffh/someFolder/*K1*.json'])
or whatever is appropriate for your platform.
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019년 10월 11일
편집: Zeynab Mousavikhamene 2019년 10월 11일
@Geoff Hayes As i mentioned in the question, the folder containing *K1*.json is differnt from matlab running script and when I use dir(['/users/geoffh/someFolder/*K1*.json']) it will check all files in the folder containing the matlab script not in the folder containing K1.json files. I need a structure that can define the folder containing K1.json files.
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일

1 개 추천

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

카테고리

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

태그

질문:

2019년 10월 11일

편집:

2023년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by