There is a specific case where having "path with space" doesn't work and I need to get the Windows short path names (e.g. Program Files = Progra~1 and Program Files (x86) = Progra~2).
Here what I'm doing for now :
[status, PATHROOT] = dos([ ...
'for %A in ("' ...
myPathWithSpace ...
'") do #echo %~sA' ...
]);
Now, I tried using regexp and regexprep to format the file path, but it fails in some case to reproduce the dos short names. So how can I reproduce the dos command with MATLAB commands?
And here my ugly try with regexp and regexprep:
PATHROOT = regexprep(regexprep(regexp(myPathWithSpace,'\w:\\\w*\s\w*\\.*','match'),'\s', ''),'(\w:\\\w{6})\w*','$1~1');
Use this function:
function shortPath = getshortpath(longPath)
fs = actxserver('Scripting.FileSystemObject');
shortPath = fs.GetFolder(longPath).ShortPath;
fs.delete;
Related
I would like to save all simulation variables ad fig with the current time.
my solution:
t = datetime('now','Format','dd-MM-yyyy''_T''HHmmss');
t2 = datevec(t);
DateString = datestr(t2);
filename=[DateString,' all_variables_main '];
save(filename )
savefig(filename)
The following error was given in Matlab:
Unable to write file 26-Oct-2019 09:47:15 all_variables_main : Invalid argument.
What have I done wrong?
mat filenames cannot have spaces or colons in them. You can use the following to directly obtain the date and time in a format that is allowed in a filename:
>> fileName = [datestr(now, 'dd-mmm-yyyy_HHMMSS') '_all_variables_main']
fileName =
'26-Oct-2019_103123_all_variables_main'
>> save(fileName)
File name containing : character is not a valid file name.
You can replace the : with "꞉" character.
See: How to get a file in Windows with a colon in the filename?
You can replace all : with ꞉ character (unicode character A789 that looks like colon) that is valid to be used in file name.
filename(filename == ':') = char(hex2dec('A789'));
Make sure to use the right character when loading the file.
Remark: The above solution was tested in Windows 10, and MATLAB R2016a.
I have a folder origin_training with subfolders like FM, folder1, folder2, ... . I can get the list of image files in .png format into a cell called FM.
FM_dir = fullfile(origin_training, 'FM');
FM = struct2cell(dir(fullfile(FM_dir, '*.png')))';
My goal is to match the names in my folder with the images in my cd, and make a new folder FM with the images from cd. Image names in the two paths are identical. I can do that with:
% mother_folder = '...' my current directory
filenames = FM(:,1);
destination = fullfile(mother_folder, 'FM', FM(:,1));
cellfun(#copyfile,filenames, destination);
This is really slow. Takes minutes even for small amounts of images (~30).
My current directory has ~ 10000 images (the ones that correspond to FM, folder2, folder3, ...). What am I missing here?
An alternative would be to create a shell command and execute it. Assuming FM contains full paths for each file, or relative paths from the current directory, then:
FM_dir = fullfile(origin_training, 'FM');
destination = fullfile(mother_folder, 'FM');
curdir = cd(FM_dir);
FM = dir('*.png');
cmd = ['cp ', sprintf('%s ',FM.name), destination];
system(cmd);
cd(curdir);
If you're on Windows, replace 'cp' by 'copy'.
Note that here we're not creating a shell command per file to be copied (presumably what copyfile does), but a single shell command that copies all files at once. We're also not specifying the name for each copied file, we specify the names of the files to be copied and where to copy them to.
Note also that for this to work, mother_folder must be an absolute path.
I somehow put my code into a function and now it works at expected speed. I suspected that cd had to do with the low speed so I am only passing the full directories as character vectors.
The same approach works for my purpose or with a slight modification to just copy from A to B. As now, it works to match files in A to those on B and copy to B/folder_name
function [out] = my_copyfiles(origin_dir, folder_name, dest_dir, extension)
new_dir = fullfile(origin_dir, folder_name);
% Get a cell with filenames in the origin_dir/folder_name
% Mind transposing to have them in the rows
NEW = struct2cell(dir(fullfile(new_dir, extension)))';
dest_folder = fullfile(dest_dir, folder_name);
% disp(dest_folder)
if ~exist(dest_folder, 'dir')
mkdir(dest_folder)
end
%% CAUTION, This is the step
% Use names from NEW to generate fullnames in dest_dir that would match
filenames = fullfile(dest_dir, NEW(:,1));
destination = fullfile(dest_folder, NEW(:,1));
% if you wanted from origin, instead run
% filenames = fullfile(new_dir, NEW(:,1));
% make the copies
cellfun(#copyfile,filenames, destination);
%Save the call
out.originDir = new_dir;
out.copyFrom = dest_dir;
out.to = dest_folder;
out.filenames = filenames;
out.destination = destination;
end
I am attempting to loop through folders, defined by variables, and select a single file, defined by a wildcard (in matlab R2012a). An example file would be: /folder1/folder2/601/mprage/xyz.nii. In researching this, I've tried to incorporate the variables and wildcard via dir and fullfile, but am getting a horzcat (conversion to struct from char is not possible) error. Ultimately, the file would be processed by the function 'callspmsegmentation'. I am new to matlab programming... Here is my script:
clear all
studyDir = '/folder1/folder2';
anatDir = 'mprage';
subjects = {'601', '602', '603'};
for jSubj = 1:length(subjects)
niiname = dir(fullfile(studyDir, subjects{jSubj}, anatDir, '*.nii'));
nii = [studyDir '/' subjects{jSubj} '/' anatDir '/' niiname];
callspmsegmentation(nii);
end
Alternatively, I tried the more direct: (which also didn't work)
clear all
studyDir = '/folder1/folder2';
anatDir = 'mprage';
subjects = {'601', '602', '603'};
for jSubj = 1:length(subjects)
nii = [studyDir '/' subjects{jSubj} '/' anatDir '/*.nii'];
callspmsegmentation(nii);
end
The output of dir is a struct not a string, so you must access the name field to get the filename
niiname = dir(fullfile(studyDir, subjects{jSubj}, anatDir, '*.nii'));
nii = [studyDir '/' subjects{jSubj} '/' anatDir '/' niiname.name];
I would also probably re-write it to use fullfile so that you don't hard-code all of those file separators. Something like this should work.
% Store the folder name
folder = fullfile(studyDir, subjects{jSubj}, anatDir);
% Get the file listing
file = dir(fullfile(folder, '*.nii'));
% Append the folder to the filename
nii = fullfile(folder, file.name);
% Process the file
callspmsegmentation(nii);
is there a command to search for a 'particular entry inside the files present in a folder' in matlab?
like if i want to search for the word "hello" in all the files present in folder A.
allFiles = dir( 'G:\folder\myfilename' );
allNames = { allFiles.name };
only lets me search for a particular file in a specific folder.:(
You can use wildcards:
allFiles = dir( 'G:\folder\myfilename\*hello*.*' );
See this answer to get you a list of all files in a directory.
Then you can use regexpi to identify any files containing the string 'hello'.
Or as Peter D points out:
I found it useful to build in regular expressions into the function.
if ~isempty(fileList)
fileList = cellfun(#(x) fullfile(dirName,x),'UniformOutput',false);
matchstart = regexp(fileList, pattern);
fileList = fileList(~cellfun(#isempty, matchstart));
end
and change the function signature to
getAllFiles(dirName, pattern) (also in the 2nd to last line)
How can I specify the actual file to process using the Run command in Notepad++.
I want for example run pdflatex using the actualfile as input, or the cs compiler, etc.
Using the entire path isn't practical, it must works with any actual file.
You can automatically add the current file using a variable in the execution string:
C:\temp\test.exe "$(FULL_CURRENT_PATH)"
The list of available variables is documented here. Examples:
FULL_CURRENT_PATH = E:\My Web\main\welcome.html
CURRENT_DIRECTORY = E:\My Web\main
FILE_NAME = welcome.html
NAME_PART = welcome
EXT_PART = .html
SYS.<var> e.g.: SYS.PATH = %PATH%
CURRENT_WORD = <selection or word under cursor>
CURRENT_LINESTR = <text of the line line under cursor>
CURRENT_LINE = <line number>
CURRENT_COLUMN = <column number>
NPP_DIRECTORY = c:\Program Files\notepad++
NPP_FULL_FILE_PATH = c:\Program Files\notepad++\notepad++.exe
You can also see the source code at RunDlg.cpp line 77 and line 26