MATLAB Answers

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

6 views (last 30 days)
Jyothi Alugolu
Jyothi Alugolu on 6 Feb 2017
Edited: Stephen on 26 Apr 2021
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,the loaded text file must form a row vector of size 58*28,if the file size is,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 me to solve this issue..
Code which i tried..
filePattern = fullfile('dir name', '*.txt');
txtFiles = dir(filePattern);
for k = 1:n;
baseFileName = txtFiles(k).name;
fullFileName = fullfile('dir name', baseFileName);
sprintf('%s\n', fullFileName);
A{k} = textread(fullFileName);
[p q]=size(A{k});
for s=1:p
for t=1:q
[m n]=size(A{s,t});
for i=1:m
for j=1:1
K=[X Y Z];
I have attached the folder of text files..
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

Stephen on 6 Feb 2017
Edited: Stephen 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)];
The structure array S has size 751x1. If you want all of the data in one numeric matrix, then do this:
Z = vertcat(;
Note that Z has size 39977x28, because every file has multiple lines of data.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!