Clear Filters
Clear Filters

How to read a sequence of files with same extension and store it

15 views (last 30 days)
Hello Everyone, I have N number of text cards, lets say 200 cards with similar extension, the extension is (07), I wrote a code to open each card individually and store it in vectors with fixed width columns. Now to import or export multiple files, how can I create a control loop to process one file at a time Automatically? knowing that each card has the same number of columns but different rows, Example one has 2500 rows and other has 1700 . %to read each card I wrote:
fileID = fopen('C77901XX.07','r');
formatSpec = '%s';
A_cell = textscan(fileID,formatSpec);
for i=1:size(A_cell{1},1)
A(i,:)=char(A_cell{1,1}{i});
end
and the code to separate columns is :
y=A;
[rows,columns]=size(A);
% % This for loop is to read the data row by row and store in different
% % matrices
for i=1:rows
Record_Type(i,:)=y(i,1:1);
FIPS_State_Code(i,:)=y(i,2:3);
Station_ID(i,:)=y(i,4:9);
Direction_of_Travel_Code(i,:)=y(i,10:10);
Lane_of_Travel(i,:)=y(i,11:11);
Year_of_Data(i,:)=y(i,12:13);
Month_of_Data(i,:)=y(i,14:15);
Day_of_Data(i,:)=y(i,16:17);
Hour_of_Data(i,:)=y(i,18:19);
Total_Volume(i,:)=y(i,20:24);
Class_1_Count (i,:)=y(i,25:29);
Class_2_Count (i,:)=y(i,30:34);
Class_3_Count (i,:)=y(i,35:39);
Class_4_Count (i,:)=y(i,40:44);
Class_5_Count (i,:)=y(i,45:49);
Class_6_Count (i,:)=y(i,50:54);
Class_7_Count (i,:)=y(i,55:59);
Class_8_Count (i,:)=y(i,60:64);
Class_9_Count (i,:)=y(i,65:69);
Class_10_Count (i,:)=y(i,70:74);
Class_11_Count (i,:)=y(i,75:79);
Class_12_Count (i,:)=y(i,80:84);
Class_13_Count (i,:)=y(i,85:89);
Class_14_Count (i,:)=y(i,90:94);
Class_15_Count (i,:)=y(i,95:99);
end
how can I please do this to the whole cards by one click? and by the way I wrote this to find the cards with the same extension in the folder:
path =pwd;
extension = '.07'
rtnFiles=dir(fullfile(path,['*' extension]))
Thank you guys

Answers (1)

KSSV
KSSV on 16 Oct 2017
To read all the .07 files:
myfiles = dir('*.07') ;
N = length(myfiles) ; % total number of files
data = cell(N,1) ; % to store the data of each file into cell
% loop for each file
for i = 1:N
thisfile = files(i).name ;
% read the file/ do waht you want / you may run your code here
% data{i} = file data ; % store file data like this into cell
end
  3 Comments
KSSV
KSSV on 17 Oct 2017
Copy your code here.....files should be a structure of all files with extension.07 present in the folder.
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD on 17 Oct 2017
Ok this is my code:
fileID = fopen('C77901XX.07','r');
formatSpec = '%s';
A_cell = textscan(fileID,formatSpec);
for i=1:size(A_cell{1},1)
A(i,:)=char(A_cell{1,1}{i});
end
and the code to separate columns is :
y=A;
[rows,columns]=size(A);
% % This for loop is to read the data row by row and store in different
% % matrices
for i=1:rows
Record_Type(i,:)=y(i,1:1);
FIPS_State_Code(i,:)=y(i,2:3);
Station_ID(i,:)=y(i,4:9);
Direction_of_Travel_Code(i,:)=y(i,10:10);
Lane_of_Travel(i,:)=y(i,11:11);
Year_of_Data(i,:)=y(i,12:13);
Month_of_Data(i,:)=y(i,14:15);
Day_of_Data(i,:)=y(i,16:17);
Hour_of_Data(i,:)=y(i,18:19);
Total_Volume(i,:)=y(i,20:24);
Class_1_Count (i,:)=y(i,25:29);
Class_2_Count (i,:)=y(i,30:34);
Class_3_Count (i,:)=y(i,35:39);
Class_4_Count (i,:)=y(i,40:44);
Class_5_Count (i,:)=y(i,45:49);
Class_6_Count (i,:)=y(i,50:54);
Class_7_Count (i,:)=y(i,55:59);
Class_8_Count (i,:)=y(i,60:64);
Class_9_Count (i,:)=y(i,65:69);
Class_10_Count (i,:)=y(i,70:74);
Class_11_Count (i,:)=y(i,75:79);
Class_12_Count (i,:)=y(i,80:84);
Class_13_Count (i,:)=y(i,85:89);
Class_14_Count (i,:)=y(i,90:94);
Class_15_Count (i,:)=y(i,95:99);
end
Note in the code I wrote C77901XX which is the file ID, I need this code to write all the files that have the extension of 07 and store each file outputs in a separate file please

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!