picking up data file in each iteration
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
tareq ALTALMAS
el 9 de Sept. de 2019
Respondida: tareq ALTALMAS
el 10 de Sept. de 2019
hi every one;
i hope i can find a solution to my probem
i need to analyze 200 speech data files
they are saved in 200 data files .mat (ex: s1018.mat, s1028.mat .....and so on)
i want to analyze all these data files one by one
so how can automatically pick file in every itteration inside for loop ?
thanks
3 comentarios
Respuesta aceptada
Stephen23
el 10 de Sept. de 2019
Editada: Stephen23
el 10 de Sept. de 2019
"so how can automatically pick file in every itteration inside for loop ?"
Following the outlines given in the MATLAB documenatation:
However, based on your comment here , it appears that unfortunately the data are very badly designed, because the variable names are not the same for all .mat files (contrary to what many beginners think, it is much easier to process multiple .mat files in a loop when their variable names are all exactly the same).
Luckily for you, there is an easy solution if each .mat file contains only one variable, then you can import the file data like this:
D = 'path of the directory where the files are saved';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
for k = 1:N
T = load(fullfile(D,S(k).name));
C = struct2cell(T);
V = C{1}; % your vector
... do whatever you want with V
end
0 comentarios
Más respuestas (4)
Bob Thompson
el 9 de Sept. de 2019
The best way I know how to do this is by listing all of the files using dir, and then looping through the files.
flist = dir('*.mat');
for i = 1:length(flist)
fi = open(flist(i).name); % Open file
...
end
0 comentarios
Fabio Freschi
el 10 de Sept. de 2019
Editada: Fabio Freschi
el 10 de Sept. de 2019
Following Stephen link
numFiles = 200;
for k = 1:numfiles
fileName = sprintf('s%d.mat', 1008+10*k);
data = load(fileName);
end
Assuming that all files can be reached with 1008+10*k
2 comentarios
Fabio Freschi
el 10 de Sept. de 2019
you are right, I just forgot to put the output variable! Thanks for pointing out.
Answer edited.
tareq ALTALMAS
el 10 de Sept. de 2019
4 comentarios
Stephen23
el 10 de Sept. de 2019
Editada: Stephen23
el 10 de Sept. de 2019
for i = 1
signal = s1018.mat
for i = 2
signal = s1028
and so on
You call repeating that 200 times "simple" ?
"i think if the file is in the current folder the load can do the work..."
The code I gave does not require the files to be in the current folder.
"... but in my way it is already in the workspace... "
How are you going to write code that accesses those 200 different variables? Loading directly into the workspace will lead to complex code. It will not be a good use of your time.
"...so i cannot use the load"
What on earth is stopping you from using load? MATLAB does not restrict how many times you import data.
"i need to ,ake signal = vector has the values inside s1018"
That is exactly what my answer gives you.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!