MATLAB Answers

0

picking up data file in each iteration

Asked by tareq ALTALMAS on 9 Sep 2019
Latest activity Answered by tareq ALTALMAS on 10 Sep 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 Comments

have names a particular sequence?
i have in my workspace vectors names as follow ( these vectors represent speech data)
s1018
s1028
s1038
s1048
.
.
s1098
i need to perform LPC analysis in each file one by one inside for loop so every itteration the loop will pick a vectr analyze it then save it in feature vector

Sign in to comment.

5 Answers

Answer by Stephen Cobeldick on 10 Sep 2019
Edited by Stephen Cobeldick on 10 Sep 2019
 Accepted Answer

"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 Comments

Sign in to comment.


Answer by Bob Nbob
on 9 Sep 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 Comments

Sign in to comment.


Answer by Fabio Freschi on 10 Sep 2019
Edited by Fabio Freschi on 10 Sep 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 Comments

@Fabio Freschi : if you load into an output variable, then you could avoid the issue shown here.
you are right, I just forgot to put the output variable! Thanks for pointing out.
Answer edited.

Sign in to comment.


Answer by tareq ALTALMAS on 10 Sep 2019

thanks to all for your comments
the files are in the workspace already
so in every for loop itteration i would pick a .mat for the analysis
in simple
for i = 1
signal = s1018.mat
for i = 2
signal = s1028
and so on

  4 Comments

Show 1 older comment
when i implement the signal will be = the name of the mat not the values
signal = cell with the value s1018.mat
i need to ,ake signal = vector has the values inside s1018
now i understand the method provided above
i think if the file is in the current folder the load can do the work but in my way it is already in the workspace so i cannot use the load
i will try now
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.

Sign in to comment.


Answer by tareq ALTALMAS on 10 Sep 2019

fs = 8000;
Names = ['s0101';'s0102';'s0103';'s0104';'s0106';'s0108';'s0109';'s0111';'s0112';'s0114';'s0115';'s0116';'s0117';'s0118';'s0121';'s0122';
's0201';'s0202';'s0203';'s0204';'s0206';'s0208';'s0209';'s0211';'s0212';'s0214';'s0215';'s0216';'s0217';'s0218';'s0221';'s0222';
's0301';'s0302';'s0303';'s0304';'s0306';'s0308';'s0309';'s0311';'s0312';'s0314';'s0315';'s0316';'s0317';'s0318';'s0321';'s0322';
's0401';'s0402';'s0403';'s0404';'s0406';'s0408';'s0409';'s0411';'s0412';'s0414';'s0415';'s0416';'s0417';'s0418';'s0421';'s0422';
's0501';'s0502';'s0503';'s0504';'s0506';'s0508';'s0509';'s0511';'s0512';'s0514';'s0515';'s0516';'s0517';'s0518';'s0521';'s0522';
's0601';'s0602';'s0603';'s0604';'s0606';'s0608';'s0609';'s0611';'s0612';'s0614';'s0615';'s0616';'s0617';'s0618';'s0621';'s0622'];
for i=1:32
signal = load('problem_all.mat',Names(i,:));
C = struct2cell(signal);
V=C{1};
Vfilter = v_ssubmmse(V,fs);
vnormal = Vfilter./abs(max(Vfilter ));
[ar1]=v_lpcauto(vnormal ,10,[5 20],'m');
ar1 = mean (ar1);
lpccoff(i,:) = ar1;
lpccoffno1(i,1:10) = ar1(2:11);
label(i,1:2) = sprintf(Names(i,4:5));
end
labelF = cellstr(label)
i did it like that
it works

  0 Comments

Sign in to comment.