How to get MATLAB to process the files specified in batch
9 views (last 30 days)
Show older comments
How to get MATLAB to work with the files specified in batch. I have converted the program edited in matlab to .exe. I hope that batch will enable .exe to batch process any file I specify in batch. That is, I just need to change the name of the folder in batch, and .exe can batch process it.
I am now entering the path to manually locate it.
I want matlab to automatically locate the path to the folder by the folder name.
How do I do this?
cd D:\Mei\folder1;
cd %check Position
sourcepath = 'D:\Mei\folder1';
Definationpath = 'D:\Mei\2';
1 Comment
Manas
on 4 Oct 2023
To clarify, you want to figure out the complete path based on the file name right? from which directory will you be searching for this file? will there be other files with the same name?
Answers (1)
Moksh
on 14 Dec 2023
Hi Yuxiao,
I understand that you want to batch process files and you want MATLAB to automatically locate the path to the specified folder name.
For this you can try using the “SUBDIR *new*” code on file exchange. This code recursively traverses all the paths in the present working directory and stores them. Using this you can check for the required folder in the present working directory which in all possible sublocations.
Here is an example code for how you can use the “subdir” function:
% Using the subdir function to list all possible paths recursively in the present working directory
P = subdir();
% Folder to search for
folderName = 'MyDataFolder';
found = false; % Flag if folder is found
pathToUse = ""; % Final path to use if folder is found
% Iterate over all possible folder and subfolder paths in present working directory
for i = 1:length(P)
potentialPath = fullfile(P(i), folderName);
disp(potentialPath{1})
% If path is found store it in pathToUse variable
if exist(potentialPath{1}, 'dir')
found = true;
disp("Path found");
disp(potentialPath{1});
pathToUse = potentialPath{1};
break
end
end
% If folder does not exist in the present working directory
if ~found
disp([folderName, " not found"]);
else
% Run the batch file on the path where folder is found
% batch (pathToUse)
end
Please note that the function “subdir” will only run after you have downloaded the source code for “SUBDIR” from file exchange. Please refer to the following link to file exchange.
Further, please run the above code in the directory which potentially contains the required folder.
Please refer to the following documentation for more information about the above used functions:
- "fullfile" - https://www.mathworks.com/help/matlab/ref/fullfile.html
- "exist" - https://www.mathworks.com/help/matlab/ref/exist.html
I hope this information helps resolve the query.
Best Regards,
Moksh Aggarwal
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!