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

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.

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

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

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

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

concatenating file path to changing folders

I am somewhat new to MATLAB and am trying to set up a changing file path in a loop to go into a series of folders and grab image files from each folder. I'm not sure if the problem is with the concatenated parts of the path itself, or with the wildcard search I am using.
I've used similar changing file paths before that have worked, but this one is giving me a "Index exceeds matrix dimensions" error. I thought it was the '*' element that was problematic (similar concatenated paths have worked for me, but only when I specify a file extension or part of a file name), but I am trying to grab DICOM files that do not have any extension, which might make it difficult.
The line within the for loop is as follows:
inputs{1, crun} = cellstr(spm_select('FPList'[allinput,'T1Rawunzip',filesep,OrderForDicoms3{crun,1}],'*'));
I've tried different ways of specifying this--using spm_select, not using spm_select, using commas instead of filesep or vice versa, but nothing has worked.
Any advice would be very much appreciated.
(for reference:
crun is the counter the moves the loop forward, 'allinput' is a previously-specified path, OrderForDicoms3 is a .mat file with a list of folder names that are being individually concatenated to the path each time the loop runs)
Thanks!
-Victoria
I can tell you the most general approach of grabbing files from a folder. If you specify the input folder through uigetdir, then all the files can be grabbed using dir command:
folder = uigetdir;
files = dir(folder);
for i =1:length(files)
if(~files.isdir())
filename = fullfile(folder, files(i).name);
% ... read in the data %
end
end
You can always do it for multiple levels.