Reading multiple txt data
1 view (last 30 days)
Show older comments
Hello
I am a kinda newbie in matlab and want to know how to read multiple txt-files in my function. I am using Matlab R2017a, windows 10.
Whole code below. It is reading only 1 txt-file(last one), but i want to read all 74 files
txtfiles = dir('C:\Users\User\Documents\MATLAB\signal\signal*.txt');
ttrigger = dir('C:\Users\User\Documents\MATLAB\signal\trigger*.txt');
N = length(txtfiles) ;
for k = 1:N
thisfile = txtfiles(k).name ;
Tthisfile = ttrigger(k).name;
s = csvread(thisfile);
t = csvread(Tthisfile);
r=0;
j=1;
n=0;
loc=0;
q=0;
lock=0;
inPulseS=false;
inPulseT=false;
for i=1:length(s)
if t(i)>3 && inPulseT==false
n=n+1;
loc(n)=i;
inPulseT=true;
elseif t(i)<3
inPulseT=false;
end
if s(i)<=-5 && inPulseS==false
q=q+1;
lock(q)=i;
inPulseS=true;
elseif s(i)>-5
inPulseS=false;
end
end
g=1;
for v=1:length(loc)-1
next=lock(lock>loc(v) & lock<loc(v+1));
if length(next)==1
diff(g)=next-loc(v);
g = g+1;
end
end
filter=diff(diff>100 & diff<400);
kk=0;
rr=1;
for h=1:length(filter)-1
if filter(h)<200
kk(rr)=0;
rr=rr+1;
else
kk(rr)=1;
rr=rr+1;
end
end
end
0 Comments
Accepted Answer
dpb
on 17 Dec 2019
txtfiles = dir('C:\Users\User\Documents\MATLAB\signal\signal*.txt');
ttrigger = dir('C:\Users\User\Documents\MATLAB\signal\trigger*.txt');
N = length(txtfiles) ;
for k = 1:N
thisfile = txtfiles(k).name ;
Tthisfile = ttrigger(k).name;
s = csvread(thisfile);
You have a specific directory in the file name spec for the dir() call, but only use the name in the loop to open the file(s). Unless the current working directory is the same as that, above, it will not find the file (and even if there happens to be one of the same name, it won't be the same one as found).
rtdir='C:\Users\User\Documents\MATLAB\signal'); % set the target folder name
sdir=dir(fullfile(rtdir,'signal*.txt')); % dir() signal files
tdir=dir(fullfile(rtdir,'trigger*.txt')); % dir() trigger files
N = length(sdir); % assumes same number of each...
for k = 1:N
s=csvread(fullfile(sdir(k).folder,sdir(k).name)); % read fully-qualified name
t=csvread(fullfile(tdir(k).folder,tdir(k).name)); % read fully-qualified name
...
The above still overwrites the s,t variables on each pass which is why it looks like it only reads one...if the above caveat on directory location is met, then it will operate on al N (of course, it still operates on N even if N is just one).
Do whatever it is you wish to do with each set of files (including save whatever results you want for each) before going on to the next. You don't say what the expected result really is...
4 Comments
dpb
on 18 Dec 2019
Edited: dpb
on 18 Dec 2019
Did you try it???? It is a vector of 0's and 1's; you can easily replace the loop with a logical addressing expression:
%d=randi(2000,1,40); % make up some sample data
d=[1630 1812 254 1827 1265 196;
557 1094 1916 1930 316 1942;
1915 971 1601 284 844 1832;
1585 1919 1312 72 1699 1868;
1358 1516 1487 785 1311 343;
1413 64 554 93 195 1647;
1390 635 1901 69];
Let's compare with above data--
>> filter=d(d>100 & d<400);
kk=0;
rr=1;
for h=1:length(filter)
if filter(h)<200
kk(rr)=0;
rr=rr+1;
else
kk(rr)=1;
rr=rr+1;
end
end
>> kk
kk =
1 0 1 1 1 0
>> mykk={~(d(d>100 & d<400)<200)}
mykk =
1×1 cell array
{1×6 logical}
>> mykk{:}
ans =
1×6 logical array
1 0 1 1 1 0
>> all(kk==mykk{:})
ans =
logical
1
>>
which shows are identical...
NB: Your original code only returns a vector of length 5, however, not 6 as your upper loop runs over only filter(1:end) so will miss the last element every time. I'm thinking this is probably not deliberate but if is, just use the indexing expression on the array inside the logical expression to duplicate behavior.
NBSecond: The locations in the vector have nothing to do with the original positions of those elements that satisfy the test, though, so it's not clear what the intent of it is? And, of course, there may well be a different length vector for each case as well...if you want the location in the original vector, you should return the positions found in the original instead of the truncated vector. The logical addressing vector is only good in comparison to the shorter subset...which might be ok excepting you're not saving it to compare into. As noted, it just isn't at all clear as to what this might mean in the end.
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!