Processing files using a for loop
Mostrar comentarios más antiguos
I am trying to write a program to read in files and analyze each file one by one. The files are wav files, and I want to read them in, filter them with a filter I have already designed, plot frequency vs. time and do a spectrogram of each file. I am supposed to use uigetdir to find the directory of files I want to read into MATLAB, and then analyze each one, hopefully with a 'for loop'. I'm not too advanced, so this may be easy and I'm just missing how to do it. Any help would be appreciated.
Respuesta aceptada
Más respuestas (1)
Kevin Holst
el 21 de Feb. de 2012
something like this:
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.wav'); %gets all wav files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[wavData, Fs] = wavread(fullFileName);
% all of your actions for filtering and plotting go here
end
9 comentarios
Joel Sande
el 15 de En. de 2016
You know what ? this code saved my life.
SANKAR JYOTI NATH
el 26 de Oct. de 2016
sir what will be the function if the files are in .txt format?
Walter Roberson
el 26 de Oct. de 2016
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.txt'); %gets all txt files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
num = importdata(fullFileName); %or readtable
% all of your actions for filtering and plotting go here
end
However, .txt files can be complex, and importdata might not be able to handle the format your .txt file is in.
Andrew Smelser
el 21 de Feb. de 2020
Editada: Andrew Smelser
el 21 de Feb. de 2020
This is awesome. A few notes though, There is a typo omitting a closing parenthese and I believe "myFolder" should be replaced with "myDir". I modified the original code you posted to read a bunch of PDF files; it doesn't do anything with them (yet) but for anyone who may get stuck on why it wasn't working here ya go. (R2018b)
Edit 1: Changed some wording in comment to make it more clear
% Just prints to command window the files in a
% directory matching the given extension
%
% Original post:
% https://www.mathworks.com/matlabcentral/answers/29837-processing-files-using-a-for-loop#answer_38248
myDir = uigetdir; % gets directory
myFiles = dir(fullfile(myDir,'*.m')); % Added closing parenthese!
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName); % Changed myFolder to myDir
fprintf(1, 'Now reading %s\n', fullFileName);
end
Bibek Dhami
el 28 de Jul. de 2020
Hi I am using this code to process several files on a folder. I want to get the surf plot out from all the files in the folder. If I process each file individually I am getting it but using this I am getting some error mentioning
Error in surf (line 139)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
Error in Untitled8 (line 9)
surf(fullFileName,'edgecolor','none');
%%%%%%%%%%%%%%%%%%%%%%
My code is as follows
myDir = uigetdir; % gets directory
myFiles = dir(fullfile(myDir));% Added closing parenthese!
for k = 3:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName); % Changed myFolder to myDir
fprintf(1, 'Now reading %s\n', fullFileName);
surf(fullFileName,'edgecolor','none');
end
But If just use a single file from folder it works
like
A=load('1');
surf(A,'edgecolor','none');
Can anyone guide me on this?
Christian Kunis
el 24 de Jun. de 2021
You have to load the data in the loop, not just pass the filename to the surf() function.
For example:
...
fprintf(1, 'Now reading %s\n', fullFileName);
A=load(fullFileName);
surf(A,,'edgecolor','none');
end
(I know this is an old post, I felt like responding anyway. Hopefully it will help someone in the future)
Deepu S S
el 10 de Sept. de 2021
i'm also working on the above shown code. I'm facing some problems .Actually my aim is to read and plot all file ina single axis or subplots. please help me
function CHD_Multiview_Callback(hObject, eventdata, handles)
set(handles.chdprevious, 'Visible','on')
set(handles.chdnext,'visible','on')
set(handles.chdtext,'visible','on')
set(handles.chdpanel,'visible','on')
guidata(hObject, handles);
disp('Entered');
guidata(hObject, handles);
folder_name = uigetdir('','Select src data Directory');
if isequal(folder_name,0)
disp('Directory Selected');
else
% set(handles.EEGFilename,'string','Wait Loading File.......');
disp(['You have selected ', fullfile(folder_name)]);
disp('CHD Audio file processing wait')
handles.srcFolder= fullfile(folder_name);
% set(handles.edDirName,'string',handles.srcFolder);
end
guidata(hObject, handles);
% Specify the folder where the files live.
k='';
myFolder = 'F:\audio';
folder ='F:\';
% AudioArray{k} = audioread(fullfile(folder));
% maxaudio(k) = max(AudioArray{k});
% myFolder = 'F:\audio';
% 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, '**/*.wav'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% fdispf(1,'Now ploting chd %s\n',fullfile);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
AudioArray = audioread(fullFileName);
% [y,fs] = audioread(fullFileName);
% subplot = 'theFiles';
end
In this for loop i want tyo add function to plot all readed audio files .So guys please help me to solve this issue...............
Deepu S S
el 10 de Sept. de 2021
Editada: Walter Roberson
el 10 de Sept. de 2021
surf(A,,'edgecolor','none');
why using this function i'm very new to MATLAB
Compare:
A = sort(randi([-2 9], 300, 500));
surf(A)
title('edgecolor default')
figure
surf(A, 'edgecolor', 'none')
title('edgecolor none')
Notice that the first of the two surface plots is nearly completely black, but the second of them, with edgecolor none, looks fine.
The difference is that in the first one, all the edges have been drawn in black. But edges have fixed drawing width: if you draw an edge and then zoom the plot in or out, the edge stays the same thickness on the screen. When you have enough data that the view has to zoom out to look at it all, then the data coordinates get squished together compared to physical drawing coordinates, so the data coordinates at which the edges get drawn get closer together, but the width of the edges stays the same. At some point, the edges are pretty much touching, and all you can see is the edges.
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

