Loop to read in CSV Values by incrementing a part of the Filename

Hello,
I have need to read in several hundred .CSV files using xlsread. The filenames are the same, except for one number at the end.
The files are all kept in the same folder.
I am currently using:
BAM_186_Torque_8Nov_Values = [ xlsread('8NOV12 -- 0656.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0657.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0658.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0659.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0700.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0701.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0702.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0703.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0704.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0705.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0706.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0707.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0708.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0709.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0711.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0712.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0713.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0714.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0715.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0716.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0717.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0718.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0719.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0720.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0721.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0722.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0723.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0724.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0725.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0726.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0727.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0728.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0729.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0730.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0731.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0732.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0733.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0734.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0735.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0736.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0737.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0738.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0739.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0740.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0741.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0742.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0743.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0744.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0745.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0746.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0747.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0748.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0749.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0750.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0751.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0752.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0753.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0754.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0755.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0756.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0757.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0758.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0759.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0800.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0801.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0802.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0803.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0804.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0805.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0806.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0807.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0808.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0809.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0810.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0811.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0812.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0813.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0814.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0815.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0816.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0817.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0818.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0819.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0820.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0821.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0822.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0823.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0824.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0825.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0826.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0827.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0828.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0829.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0830.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0831.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0832.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0833.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0834.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0835.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0836.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0837.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0838.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0839.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0840.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0841.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0842.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0843.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0844.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0845.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0846.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0847.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0848.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0849.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0850.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0851.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0852.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0853.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0854.CSV',1,'G15:G1014')'; xlsread('8NOV12 -- 0855.CSV',1,'G15:G1014')';
xlsread('8NOV12 -- 0856.CSV',1,'G15:G1014')';
];
Is there a way to make a loop to do this for me?
Thank you!

1 Kommentar

Image Analyst
Image Analyst am 9 Nov. 2012
Bearbeitet: Image Analyst am 9 Nov. 2012
This is covered by the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F Just minor modifications to the demo code there and you'll be on your way.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Dr. Seis
Dr. Seis am 9 Nov. 2012
Bearbeitet: Dr. Seis am 9 Nov. 2012
Should be able to do that:
M = 201; % Number of CSV files
N = 1000; % Number of datapoints in each CSV
BAM_186_Torque_8Nov_Values = zeros(M,N); % Intialize output dataset
for ii = 1 : M
filename = sprintf('8NOV12 -- 0%d.CSV',655+ii); % Create string for each file
tempdata = xlsread(filename,1,'G15:G1014')'; % Grab data from file
BAM_186_Torque_8Nov_Values(ii,:) = tempdata; % Put into output dataset
end

3 Kommentare

Dr. Seis
Dr. Seis am 9 Nov. 2012
Bearbeitet: Dr. Seis am 9 Nov. 2012
You will have to get creative if you have numbers that are greater than 999 and less than 100 (i.e., you will need to set up your file naming line for conditions other than 3-digit numbers if you "pad" those numbers with 0's in your filenames).
And you can actually skip the "tempdata" line... you don't really need an intermediate step, I just thought it might describe what is going on better.
Elige,
I found a better solution which reads all CSV files in a folder:
source_dir = 'C:\Users\borowski.m.3\Documents\BAMs\Fuzzy Logic Control of BAM\BAM 186 Startup Torque Trends'; source_files = dir(fullfile(source_dir, '*.CSV')); for i = 1:length(source_files) BAM_186_Torque_8Nov_Values = xlsread(source_files(i).name,1,'G15:G1014'); end
Only problem I am having now is, how do I put these into a length(source_files) x length('G15:G1014') matrix? (in this case, a 120x1000).
Good job... even better. As to your current problem, all you need to do is initialize your BAM_186_Torque_8Nov_Values martix and fill it like I did above. It looked like you wanted each row of data to represent each G-column data from each CSV file... so you would:
source_dir = 'C:\Users\borowski.m.3\Documents\BAMs\Fuzzy Logic Control of BAM\BAM 186 Startup Torque Trends';
source_files = dir(fullfile(source_dir, '*.CSV'));
M = length(source_files);
N = 1014-15+1; % Hard coded since 'G15:G1014' is hard coded too
BAM_186_Torque_8Nov_Values = zeros(M,N); % Intialize output dataset
for ii = 1 : M
BAM_186_Torque_8Nov_Values(ii,:) = ...
xlsread(source_files(ii).name,1,'G15:G1014')';
end
Let me know if that doesn't work.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Import and Export finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by