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!