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

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

"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?
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

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

could you be more specific for the last line out{k}=reshape(de2bi(mat.',9).',[],27);
@Jyothi Alugolu: I removed that line, please check the code again.
yeah it worked..thank you soo much..
Now,how to check whether the final output is appropriate one or not? what i mean is whether all the calculations are correct or not..and also please tell me how to access a file from it..
can you tell me how to save the output with corresponding filename..each file must save with it's file name..so,that i can access with it's corresponding name..
the output is saving with random text files..it was not saving from the starting file to end of the file..calculations are being done on random textfiles.i.e,if my folder contains 1 to 100 files,the ouput must be saved by it's filename..but,according to your logic,first it is doing binary conversion on last file i.e on 100th file ..but,i want to save with corresponding filename from starting..can u please solve this issue?
"how to check whether the final output is appropriate one or not"
Pick some test cases and calculate them by hand. Compare your answer with the answers that the code gives you.
"calculations are being done on random textfiles"
Computers don't do things randomly. Most likely you have numbers in your filenames, is this correct? If so then dir will return the files in ASCIIbetical order, and not the order of the number values. This is not a problem with my code, it is simply the order that OS's return the file names in.
I have a simple solution for you: download my FEX submission natsortfiles:
P = '....';
S = dir(fullfile(P,'*.txt'));
S = natsortfiles(S);
for k = 1:numel(S)
F = fullfile(P,S(k).name);
M = dlmread(F);
...
end
If this does not resolve your question then please give examples of the exact filenames, and explain what order you want the files to be in.
i want to save the text files with it's name..here i am sending you the textfiles..i want to save from 1_1 .... 100-8 by order...
sorry,what i mean is,in C,it was saving with filesize,that filesize must show which file name it is..i.e,i want to save it with filename instead of file size,so that accessing a filename could be easy rather than by it's file size..because there can be more than a file with same file size..In the output,the first file is 100_8...100_1,next 10_1..10_8,next 11_1..11_8..but,what i want is from 1_1 to 100_8 in linear order...i hope u understand what i am speaking
@Jyothi Alugolu: what does "from 1_1 to 100_8 in linear order" mean? Please actually help me by being clear about the order that you want. Which of these?:
  • 1_1, 1_2, 1_3, 1_4, ... 2_1, 2_2, 2_3, 2_4, ...
  • 1_1, 2_1, 3_1, 4_1, ... 1_2, 2_2, 3_2, 4_2, ...
Sorry, i want in these order..1_1,1_2,1_3...2_1,2_2.... and also i want row vector for a file which means that,if file size is 58*28 in C,then i want to concatanate all the 58 rows one by one i.e the file size should be 1624*1 (1624 is 58*28).. Finally what i want is,a file should be saved with it's filename as 1_1,1_2...100_8..and inturn each file should contain a row vector which means concatenation of all rows ..
@Jyothi Alugolu: exactly as I already wrote, you can use my FEX submission natsortfiles to get the file order that you want. And a simple reshape will put the data into one row.
P = 'a';
S = dir(fullfile(P,'*.txt'));
S = natsortfiles(S);
for k = 1:numel(S)
F = fullfile(P,S(k).name);
M = dlmread(F);
T = [de2bi(M(:,1),9),de2bi(M(:,2),10),de2bi(M(:,3),9)];
S(k).data = reshape(T.',1,[]); % or [],1 for a column vector
end
Note that this gives a row vector, which is what the title of your question asks for. If you want a column vector, which is what your comment asks for, then swap the [] and 1 around.
I am sure that you can then manage writing the data to file yourself.
while using natsortfiles to get the file in order..i am getting an error ..the error is:Undefined function 'natsortfiles' for input arguments of type 'cell'.
@Jyothi Alugolu: have a look at my comment where I write the name natsortfiles: do you see that it is a blue-colored link? When you click on that link it takes you to another page (on MATLAB File Exchange). In the top right corner of that new page is a dark blue button that says "Download Zip" on it. Click on that button. Unzip the zip file. Put the files in your current directory. Then you can use them.
though i ama using natsortfiles function,still i am not getting the order in which i want...i have 1_1,1_2,1_3..1_8,2_1,2_2,2_3..2_8.....100_1,100_2,100_3...100_8.
sorry, i got..thank you... and one more question...i have 4 binary(0's and 1's ) cells of sizes 1*1120,1*1344,1*868,1*812...now, i need to split or do partition on each cell i.e the entire cell must divide into each 8 bits,so that output of cell's must be of size first cell:8*140, second cell: 8*168, third cell must be 8*109..109 because 108 columns contains 108*8=864 binary numbers,and there will remain 4 binary number's..these 4 binary numbers must store in another column i.e 109th column..so third cell size must be 8*109..and fourth cell size must be 8*102 ...and finally i need to calculate decimal value for each splitted file..in case of 3rd cell,the 109th column contains 4 elements,these 4 elements also must convert into decimal value...final decimal vector must contain size of 1*140,1*168,1*109,1*102....
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)

Asked:

on 6 Feb 2017

Edited:

on 26 Apr 2021

Community Treasure Hunt

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

Start Hunting!