필터 지우기
필터 지우기

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

조회 수: 9 (최근 30일)
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 6일
편집: Stephen23 2021년 4월 26일
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
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?
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 7일
no,actually the final output must contain all files i.e 750 files,which inturn each file must contain filesize*28..

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
Jyothi Alugolu
Jyothi Alugolu 2017년 2월 15일
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..

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by