필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How do I find output as the path of any input stored in cells?

조회 수: 1 (최근 30일)
kd p
kd p 2017년 12월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
Suppose I have the main folder 'X' which has a subfolder 'a'. now a is like a linear array with n elements. Now picking any random element out of it, the output should show the path i.e, X\a\n. Like 'a' there would be columnar till z. So it would be like a rectangular matrix. Need urgent help with this
  댓글 수: 7
kd p
kd p 2017년 12월 4일
Pardon me @Walter. Actually, I had a deadline and I'm new here.
kd p
kd p 2017년 12월 4일
It is analogous to a subfolder. I used the word to describe the paths as output. Input would be like I said in the image, the output would be just the path of the element of the desired position.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 12월 4일
projectdir = fullfile('X', 'a');
dinfo = dir( projectdir );
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
numentries = length(dinfo);
random_entry_idx = randi(numentries);
random_entry_filename = dinfo(random_entry_idx).name;
random_name = fullfile(projectdir, random_entry_filename);
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 12월 5일
편집: Walter Roberson 2017년 12월 5일
projectdir = fullfile('X', 'a');
if ~exist(projectdir, 'dir')
error('Folder "%s" does not exist relative to here', projectdir);
end
dinfo = dir( projectdir );
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
numentries = length(dinfo);
if numentries == 0
error('Folder "%s" exists but has no files in it', projectdir);
end
random_entry_idx = randi(numentries);
random_entry_filename = dinfo(random_entry_idx).name;
random_name = fullfile(projectdir, random_entry_filename);
kd p
kd p 2017년 12월 5일
Thank You, Walter. Could you kindly say what does the "remove '.' and '..' parts mean? Like I have to edit something in it or totally remove them? And besides, the code is showing error in the output

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by