Probably a simple question but i currently use
sims = uigetdir ('C:\Matlab')
info = dir(fullfile(sims,'*.in'))
list = {info.name}
Which brings up the popup window where i selected my folder, in this case 'RRfiles' which then gives me the list of the files in there and away we go. I am trying to do this without having to select the folder manually.
I have tried sims = dir ('C:\Matlab\RRfiles') which does tell me how many files are in there but the answer should be 26 and it is actually 28 because of '.' and '..' so i need a way of filtering out these files just like before. but putting info = dir(fullfile(sims,'*.in')) afterwards doesnt work. Any advice would be appreicated.

 Akzeptierte Antwort

Chunru
Chunru am 11 Okt. 2021
Bearbeitet: Chunru am 11 Okt. 2021

0 Stimmen

sims = pwd; %uigetdir ('C:\Matlab')
save test.in sims % create a file
info = dir(fullfile(sims,'*.in'))
info = struct with fields:
name: 'test.in' folder: '/users/mss.system.GGGSp8' date: '11-Oct-2021 11:53:45' bytes: 199 isdir: 0 datenum: 7.3844e+05
list = {info(~[info.isdir]).name} % this will separate folders from files
list = 1×1 cell array
{'test.in'}

5 Kommentare

Cheggers
Cheggers am 11 Okt. 2021
This only works if i am inside the folder with thee .in files. I am trying to run my code from inside \matlab and within this there is the sub folder of rrfiles. I want to be able to use the files from inside the folder while not having to actually cd into it
Rik
Rik am 11 Okt. 2021
Churnu probably only used pwd because the online running tool is limited. You should use uigetdir instead.
The posted code works without cd.
Cheggers
Cheggers am 11 Okt. 2021
I am trying to do it without uigetdir so that i dont have to select the folder when i run my code.
Chunru
Chunru am 11 Okt. 2021
Rik is right. I am using pwd and a file to demonstrate the commands later. It is the line "list = {info(~[info.isdir]).name}" that remove the '.' and '..' folders.
Cheggers
Cheggers am 11 Okt. 2021
Right that makes sense now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 11 Okt. 2021

Kommentiert:

am 11 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by