Clear Filters
Clear Filters

How to get row vector for a folder of text files?

6 views (last 30 days)
Hello, I have a folder of 751 text files..Now i have to run a loop to load all text files from the directory and then perform some operations on each text file..if suppose,i loaded a text file,now from the text file,i have taken a first row.. if suppose i have text file of size 58*3.then i loaded first row as A(1,:)..again i have to convert each element from the row into their equivalent binary numbers with defined bit size..suppose,i have a row [A B C ],then i converted each element as X=de2bi(A,9); Y=de2bi(B,10);Z=de2bi(C,9)..then forms a 1*28 string..so,the loaded text file must form a row vector of size 58*28,if the file size is 58..so,finally the output must be 751*28,where 751 are total files in the folder..I am able to form row vector only for 1 text file,but i want to get row vector for a folder of text files..help me to solve this issue..
Code which i tried..
filePattern = fullfile('dir name', '*.txt');
txtFiles = dir(filePattern);
n=length(txtFiles);
for k = 1:n;
baseFileName = txtFiles(k).name;
fullFileName = fullfile('dir name', baseFileName);
sprintf('%s\n', fullFileName);
A{k} = textread(fullFileName);
end
[p q]=size(A{k});
for s=1:p
for t=1:q
[m n]=size(A{s,t});
L=A{s,t};
%L=R{1,1};
for i=1:m
for j=1:1
N=L(i,j);
B=L(i,j+1);
C=L(i,j+2);
X=de2bi(N,9);
Y=de2bi(B,10);
Z=de2bi(C,9);
K=[X Y Z];
Q(i,1:28)=K;
end
end
end
end
I have attached the folder of text files..
  2 Comments
Stephen23
Stephen23 on 6 Feb 2017
Edited: Stephen23 on 6 Feb 2017
"the loaded text file must form a row vector of size 58*28,if the file size is 58..so,finally the output must be 751*28,where 751 are total files in the folder"
This makes no sense. Each file has more than one row, and you want to process all files, therefore your output matrix will also have more than 751 rows.
How are you going to magically compress 58 rows into one row in the output matrix?
Jyothi Alugolu
Jyothi Alugolu on 7 Feb 2017
no,actually the final output must contain all files i.e 750 files,which inturn each file must contain filesize*28..

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 6 Feb 2017
Edited: Stephen23 on 26 Apr 2021
P = 'absolute/relative path to where the files are saved';
S = dir(fullfile(P,'*.txt'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
M = dlmread(F);
S(k).data = [de2bi(M(:,1),9),de2bi(M(:,2),10),de2bi(M(:,3),9)];
end
The structure array S has size 751x1. If you want all of the data in one numeric matrix, then do this:
Z = vertcat(S.data);
Note that Z has size 39977x28, because every file has multiple lines of data.
  18 Comments
Jyothi Alugolu
Jyothi Alugolu on 15 Feb 2017
previously i posted same question,but i asked for only one file which is divisible by 8...but,now in my case,i want output for binary files which are not exactly divisible by 8..

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!