How to read differents folders automatically in matlab? [duplicate] - matlab

This question already has answers here:
How to get all files under a specific directory in MATLAB?
(8 answers)
Closed 5 years ago.
I have this folders :
D:/ Houses/objects/*.jpg
D:/ Towers/objects/*.jpg
D:/ Hotels/objects/*.jpg
.
.
.
.
The first folder have different name but inside those folders exists a folder that have the same name (objects) that contain different images in .jpg that i already know how to get them all.
The problem is the first folders have different names , so how can i go over those folders to get the .jpg in matlab, when they have different names and the name donĀ“t have the same prefix either ?
Thank you very much

Use dir
files = dir('D:/*/objects/*.jpg');
fullPath = arrayfun(#(i) fullfile(files(i).folder, files(i).name), (1:length(files))', 'uni', 0);

Related

Matlab: the meaning of '../' in a folder path [duplicate]

This question already has answers here:
What does two dots before a slash mean? (../) [closed]
(1 answer)
What do the dots mean in relative file path?
(5 answers)
What does a dot mean in a URL path?
(3 answers)
Closed 4 years ago.
I stumble across some MATLAB codes about data file paths, something like:
data=csvread(sprintf('../data/cdflevel%dtable.csv',cdflevels(i)));
I am not sure what '../' does. Anyone can explain it?
../ moves one folder up from the current directory, before then changing to the data directory in the parent folder. It's not specific to MATLAB, just a standard way of navigating a file system.

Changing Matlab function from calling user listed files to call all files with same extension [duplicate]

This question already has answers here:
process a list of files with a specific extension name in matlab
(4 answers)
Closed 7 years ago.
I am working with this routine below that performs the specific M routine for all listed ascii files that I list as follows:
files={'file1name.asc', 'file2name.asc', 'fileNname.asc'};
ratios=NaN*zeros(1,length(files));
for i=1:length(files)
ratios(i)=specific_m_routine(files{i});
end
How do I simply change this to call all .asc files in the directory, rather than listing each fileNname.asc?
Thanks!
I'm assuming you want to pass a filename to your specific_m_routine function? If so, you need to use dir and files(k).name rather than listing all of your files in a cell array and referring to them with files{k}.
files = dir('*.asc');
ratios = nan(1, numel(files));
for k = 1:numel(files)
ratios(k) = specific_m_routine(files(k).name);
end
Or if you want to be able to use any directory.
folder = fullfile('path', 'to', 'data');
files = dir(fullfile(folder, '*.asc'));
ratios = nan(1, numel(files));
for k = 1:numel(files)
ratios(k) = specific_m_routine(fullfile(folder, files(k).name));
end
This way will not require the files you're trying to load to be in the current directory.
As a side note, I feel like you have asked some variant of this exact question the past few days but haven't taken the time to digest the answers you have gotten. Please go back and review all of them to get a better idea of what you're trying to do.

How to include our own mfile into matlab library? [duplicate]

This question already has an answer here:
How to Set Path in Matlab?
(1 answer)
Closed 7 years ago.
I have created my own mfile and would like to add it permanently lie built in functions so that I dont have to specify path. Is there a way to do it?
You should store your .m file (either script or function) in a folder belonging to the MatLab search path.
To add your own folder to the MatLab search path you can use eiterh:
the Set Path tool, available on the MatLab Home toolbar
use the buil-in function path (from the Command Window or, for example, as part of a script)
E. g.
path(path,'c:\my_folder\my_sub_folder')
Hope this helps.
Write your file and save it and then add the containing folder to the MATLAB search path. You do not need to look for it every time.
addpath('c:/matlab/myfiles')
Note: your filename should not exist within MATLAB, otherwise your file would be the default one, when you call that command.
You can also do most of the work from command-line:
First, create a folder,
mkdir('c:/matlab/myfiles')
Add it to the top of your search path,
addpath('c:/matlab/myfiles')
and then save the search path for future MATLAB sessions
savepath c:/matlab/myfiles/pathdef.m

MATLAB: How do I copy files with a specific extension to a folder one directory above it?

I am trying to copy specific files from one folder into another folder one directory above it. I want to do this for all of the folders I have at once. Here's my file structure:
201415ContinuousForDropTeqc/StationA/201411/
This path has 25 folders labeled 5 through 30 (representing days).
In each of these 25 folders there are 3 folders named 'dat', 'RAW', 'rinex'.
I want all files ending in .14o from the RAW folder (there are many other file types in this folder as well) to be copied to the rinex folder.
I'm also hoping I can find a way to repeat this for every day in the 201411 folder. This last part isn't critical since I think can type the path manually and just run the script that copy and pastes the files I want.
I hope this was clear. I'm new-ish to MatLab.
Thank you in advance for your help!
Tiffany
You can do all that using the dir command. Check this link.
You can use it twice. first to get all the 25 folders and then to get all the files within the folder.
Days = dir('201415ContinuousForDropTeqc/StationA/201411/');
for k=3:numel(Days) %notice the 3
files = dir([Days(k).name '/RAW/*.14o']);
for n=1:numel(files)
copyfile([Days(k).name '/RAW/' files(n).name],[Days(k).name '/rinex/' files(n).name]);
end
end

how to import data from multiple folders in matlab? [duplicate]

This question already has answers here:
How to get all files under a specific directory in MATLAB?
(8 answers)
Closed 8 years ago.
I have my data in multiple folders. Let's say I have a folder containing 100 subfolders at the moment that look like this:
/folder/Re0001/vitesse
/folder/Re0002/vitesse
/folder/Re000N/vitesse
/folder/Re000N+1/vitesse
/folder/Re0100/vitesse
I want to import the vitesse file into a cell array. This is the code that i am using at the moment:
numfiles=100;
mydata=cell(1,numfiles);
for i=1:numfiles
mydata{i}=uiimport;
end
It is a working solution.
However, if it involves 100 or more files I have to specify each folders and files manually, which is very troublesome.
By the way I am new to Matlab so can you please incorporate example code with the directory given.
I did something similar few days ago. Have a look at the matlab function ls. If you are using windows system, you are all set. If your are using linux, you may need to split the results. However newer version of matlab have the strsplit function that will do the job, or you will use regular expressions.
In your case,
list = ls('/folder/*/vitesse');
will give you a list of your files.