find the path of a file in matlab - matlab

Hello I would like to locate the file 'my_file.mat' that should be somewhere inside the folder 'C:\...\mypath\folder1'.
the folder folder1 contains several subfolders and the file my_file could be in any of these subfolders.
I would like to retrieve its full path.

You want to use the which function.
mypath = which('my_file.mat')
As commented below, this assumes that your 'folder1' has been added to your search path. To add (and remove if no longer needed) 'folder1' to your search path:
my_folder_path = 'path/to/folder1'
addpath(genpath(my_folder_path))
mypath = which('my_file.mat')
rmpath(my_folder_path)

I think you are looking for genpath and which combo:
addpath(genpath(folderName));
which test.txt -all
>>
Z:\home\**\Documents\MATLAB\R2010b\bin\test.txt

Related

Cygwin or Gnuwin - Find .txt files named* copy & paste to specific directory

Okay so I want to know how I would go about doing this, using grep to locate .txt files named "cocacola1", "cocacola2", "cocacola3" & then copying them to another directory. So searching for files named "cocacola" &/even if it contains other characters within the file name to then copy them to another directory/location.
You can just use unix find. Assuming the files you're searching for are in 'source' and you want to copy to 'destination':
find source -name '*cocacola*' -exec cp {} destination \;
I put the wildcard '*' before and after cocacola since you said other characters might exist in the file name.

copy multiples files from multiples directories into one directory using MATLAB

How can I manage to copy the files from multiple directories into one target directory IN MATLAB for example if the directories are organized as follow:
directory1
sub-directory1
sub-sub-directory1-1
file1
file2
sub-sub-directory1-2
file4
thisis-my-file
sub-directory2
sub-sub-directory2-1
file
myfile
sub-sub-directory2-2
file-case1
the result should be something like this:
target-directory
file1
file2
file4
thisis-my-file
file
myfile
file-case1
here is one of many possible solutions:
First, get all subfolders of your directory1:
% define the destination folder
destinationFolder = 'c:\temp';
% genpath delivers all subfolders of the given directory
directories = genpath('C:\directory1');
% the following regular expression gets all these subfolder, seperated by ';'
directories = regexp([directories ';'],'(.*?);','tokens');
Now you can use the dir-function inside a for-loop for getting all the files in the subfolders:
for i=1:length(directories)
% you could use a wildcard, if you only want some
% specific files to be moved in to the target directory.
% (filesep is a built-in function!)
files = dir([directories{i}{1} filesep '*.*']);
% use a second loop for copying your files
for j=1:length(files)
% build the path and copy the file to the desired destination
copyfile([directories{i}{1} filesep files(j).name)], destinationFolder);
end;
end;
you can use a matlab command for copying files which is copyfile('source','destination') like
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file1.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-1/file2.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/file4.txt','target-directory')
copyfile('directory1/sub-directory1/sub-sub-directory1-2/thisis-my-file.txt','target-directory')
copyfile('directory1/sub-directory2/sub-sub-directory2-1/file.txt','target-directory')
like this way for all of your file and you get those file in your destination folder. you can also give a complete path of the each source and destination also.

rename file using matlab command

file1 = fullfile(pwd,'folder','toto.txt');
file2 = fullfile(pwd,'folder','toto2.txt.sav');
movefile(file1,file2)
=>this creates a folder named toto2.txt.sav
whereas I'm looking for renaming the file in the same directory
any idea ?
Check the documentation:
Renaming a File in the Current Folder
In the current folder, rename myfunction.m to oldfunction.m:
movefile('myfunction.m','oldfunction.m')
Thus:
o=pwd
cd('folder')
movefile('toto.txt','toto2.txt.sav')
cd(o);
Was looking for this as well. Now on a windows machine if I want to rename all files in a folder, I do:
system('rename *.* *.sav')

how can I rename a folder using Matlab script

I want to rename a folder in a path by using Matlab script:
c:\My Path\New Folder\ --> c:\MyPath\NewFolder\ %remove the spaces in the path name
got what I wanted finally to work:
system('7z e "C:\Public\test\dry?testing41013\Log?#1\max_logs_can_messages.tgz" -o"C:\Public\test\dry testing41013\Log #1\"')
had to use "?" for spaces in the first path but not the second path
You can use MOVEFILE function. It works for folders as well.
movefile('c:\My Path\New Folder\', 'c:\MyPath\NewFolder')

Matlab function call directory

So i had this issue that occurred when I ran a Matlab script. Here is an a simple example that illustrates it:
So its important to outline the file structure:
MainFolder
script.m
SubFolder
a1.csv
a2.csv
a3.csv
now say i have a script like this:
-> script.m
dir
it would simply print out the files in the folder.
Now the wierd thing, if i run the script in the Subfolder like this:
>>script
it will do this:
>> a1.csv a2.csv a3.csv
but if i do this in the folder:
>>run('C:\Users\....\MainFolder\script.m')
it will only print out
>> script.m
So obviously it is acting as if i ran it form MainFolder rather than SubFolder.
What is the point of this functionality?
The dir command shows the directory contents of Matlab's current directory, not that of where the script is located. So the script showed you the directory contents of wherever you happened to be in the Matlab command prompt when you called that script.
To get what you want, use this in the script:
dir(fileparts(mfilename('fullpath')))
Use pwd to see current dir
Use cd to change directory
Use path to see if your project folders are included in the path
Use which to see you are calling the right *.m file (in case there is multiple .m files with same name on the path)