iterate through multiple files in a folder

function [] = lanczos(A, m)
A = readmatrix('output1.txt','Whitespace',' []');
here A reads a single text file.
But i wanted A to read multiples text files(may be using for loop) stored in a folder(say graphs) and the function needs to run through all of them and display output for every text file.
can someone help me with this.

3 comentarios

Rik
Rik el 19 de Oct. de 2019
Editada: Rik el 19 de Oct. de 2019
How to process a set of files may be the most frequently asked question on this site. What have you tried so far?
Nitish Reddy Kotkur
Nitish Reddy Kotkur el 19 de Oct. de 2019
Editada: Rik el 20 de Oct. de 2019
myfolderinfo=dir('dense_gnm_random_graph');%dense_gnm_random_graph is the folder which contains text files
N=length(myfolderinfo);
for i = 3:N
thisfile = myfolderinfo(i).name
A = readmatrix('thisfile','Whitespace',' []');
and these are the errors i got
Error using readmatrix (line 148)
Unable to find or open 'thisfile.txt'. Check the path and filename or file permissions.
Error in final (line 6)
A = readmatrix('thisfile.txt','Whitespace',' []');
Rik
Rik el 20 de Oct. de 2019
The source of the error is that you entered the variable name as a char array instead of the contents of the variable.

Iniciar sesión para comentar.

Respuestas (2)

Katarina Vuckovic
Katarina Vuckovic el 20 de Oct. de 2019
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir));
M(length(myFiles),s)=zeros; %gets all wav files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
A = readmatrix(fullFileName,'Whitespace',' []');
end
Cathal
Cathal el 18 de Jul. de 2023
Editada: Cathal el 18 de Jul. de 2023
Expanding on Katarina's answer, this worked for me with some small changes:
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir, '*.txt'));
M(length(myFiles))=zeros; %gets all wav files in struct
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
fullFileName = fullfile(myDir, baseFileName);
A = readtable(fullFileName,'Whitespace',' []');
%Assuming by display you mean to plot?
plot(A{:,1}, A{:,2}, Marker=".") %Replace {:,1} and {:,2} with whatever
hold on
end

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Oct. de 2019

Editada:

el 18 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by