How to get row vector for a folder of text files?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Stephen23
2017년 2월 6일
편집: Stephen23
2017년 2월 6일
"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?
채택된 답변
Stephen23
2017년 2월 6일
편집: Stephen23
2021년 4월 26일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!