load multiple specific folders from a directory in MATLAB/Extract file type from multiple different folders in a directory - matlab

I am wondering how to load multiple specific folders from a directory in MATLAB.
Each of these specific folders, have sub-folders(all named the same), and within these subfolders are .mat files I need to extrapolate and compile.
Essentially, I just need to get the .mat files from the entire directory.
I can get the all .mat files from a folder, but don't know how to get all the .mat files from the main directory.
I realize I could use a script that loads each folder individually; essentially copying and pasting the same script for each folder and editing the variables.
However, it would be much more useful if I could just extract the .mat files from the entire directory.
Thanks

Related

How to move files whose file name is not used in a set of text files?

I'm a Powershell beginner and this is my first post on stackoverflow. I can understand some simple pipelines, but the following challenge is too complicated for me at this point:
I have a folder with testdata containing *.bmp files and their associated files. I want a powershell script to check which bmp-files are still used. If not used, move bmp-files and associated files to another folder.
Details:
bmp-files and associated files: For example; car01.bmp, car01.log, car01.file, car02.bmp, (...)
The bmp-files are in use if their file name (eg, car01.bmp) is mentioned in any of the (text/csv) files in at least one of 2 locations (incl. subfolders).
If the file name is not found in any of the text files, I want the script to move that file, and any file who's name differs only by file extension to a designated folder.
Looking forward to your solutions!

Make a copy of the files in a directory but save them as a different file type

Is it possible to list all of the files in a directory of a certain file type, and then save them in the same directory but as a separate file type using MatLab?
In my case, I have 144 files saved as in a .fig format, but I would like to copy them as a .tif format so that I don't have to go and change each file manually.
I know I can list all the files in my directory using the dir function and I guess I could simply run for loop with i=1:length(dir) but I don't know how to isolate the files of a specific file type. I don't see filetype as a field name on the mathworks website for dir.
Thanks for any suggestions.
To list only files of type .fig:
files = dir('*.fig');
You can then loop over the names:
for k = 1:numel(files)
filename = files(k).name;
% do something with filename
end

Renaming files in Matlab - movefile is creating folders instead of files

The current working directory contains a folder called 'dynamics_sorted' which contains 300 subfolders ('001', '002', etc), each of which contains some files, but only a single nifti (.nii) file.
The single nifti file from each of the numbered subfolders should be moved into 'dynamics_sorted_NIFTI' which is in the current working directory.
In the process, each nifti file should be renamed with the number of its parent folder.
The syntax for movefile suggests that when the arguments are both filenames then the file is renamed
http://uk.mathworks.com/help/matlab/ref/movefile.html#zmw57dd0e528520
for Ticker = 1:300;
FindNiftiFile = ['dynamics_sorted/',num2str(Ticker,'%03.0f'),'/*.nii'];
PutNiftiFile = ['dynamics_sorted_NIFTI/',num2str(Ticker,'%03.0f'),'.nii'];
movefile(FindNiftiFile,PutNiftiFile);
end
But this code does not rename the files, instead it keeps the filenames but places them into numbered folders.
Any advice as to where the error is?
I've found the answer - it's because of the wildcard used to find the source file. I'm guessing this leads Matlab to assume the source is not a single file, even when the wildcard is such that only a single file is eligible.

How to compare with Robocopy and save files in different directory

I would like to compare two folders, and the differences between them, I would like to save in a third directory, keeping the original file-tree.
Can I do this with Robocopy?
Thank you

How to read trc files from different directory in matlab

I have a program that reads data from .trc files. But as of now, it can only read the .trc files that are in the current directory i.e the MATLAB folder. If I want it to read a file, I have to copy the file in MATLAB folder. Is there a way I can move to different directories and choose another .trc file? I have tried using dir, uigetdir etc. but nothing seems to work!
It's pretty straightforward to filter results with the dir command with a file extension if you specify the folder to search with a wildcard character in place of the file name. You can then loop over the generated list of file names in the output structure (as shown) or whatever you need to do with those files.
folderName = 'C:\Path\To\Target\Folder\';
fileList = dir(strcat(folderName, '*.trc'));
for k = 1:length(fileList)
fileHere = fullfile(folderName, fileList(k).name);
% Do what you need with the files in here
end