how to avoid CDing a text file in a dir

I have 100s of folders that i need data from but part of the main folder theres txt file that i need to bypass..
when my code CDs the text file it crashes. how may i avoid this
paramater_data_folder = [starting_point,'\',folder_to_pass,'\']; % data folder
cd(paramater_data_folder)

5 commentaires

Stephen23
Stephen23 le 3 Jan 2023
"how to avoid CDing a text file in a dir"
Every MATLAB function that imports/exports/processes data files will accept absolute/relative filenames. Using absolute/relative filenames is simple and efficient. In contrast, using CD is slow and makes debugging harder.
Ihaveaquest
Ihaveaquest le 3 Jan 2023
sorry not sure how that helps me
Stephen23
Stephen23 le 3 Jan 2023
Modifié(e) : Stephen23 le 3 Jan 2023
"sorry not sure how that helps me"
Your question is entitled "how to avoid CDing a text file in a dir". Ergo, I just told you the best way to avoid CDing to any folder: use absolute/relative filenames. Don't use CD at all. CD is not required for processing data files.
Ihaveaquest
Ihaveaquest le 3 Jan 2023
i see, so you are saying that cd does not have to be used
do you have an examle of what you mean ???

Connectez-vous pour commenter.

 Réponse acceptée

Try this
% Specify the folder where the files live.
myFolder = pwd; %or 'C:\Users\yourUserName\Documents\My Pictures' or wherever.
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.txt'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
% Loop over all filenames.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
% Skip the bad file, which has a known filename.
if strcmpi(baseFileName, 'some really bad file.txt')
continue; % Skip to bottom of loop and keep iterating with remaining files.
end
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an array with readmatrix()
% imageArray = readmatrix(fullFileName);
end
Adapt as needed.
FAQs:
https://matlab.fandom.com/wiki/FAQ#How_can_I_process_a_sequence_of_files?

2 commentaires

Image Analyst
Image Analyst le 3 Jan 2023
@Ihaveaquest isn't this code " an examle of what you mean"??? If not, why not?
Ihaveaquest
Ihaveaquest le 3 Jan 2023
it is - i just wasnt sure what the other user was talking about

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by