How do you pull select columns from dozens of .csv files in a directory and make one long vector with all the values?

How do you pull select columns from dozens of .csv file in a directory and make one long vector with all the values?
Each .csv file has the required data in the same location.

Risposte (1)

% I'll assume that your files are named follwing some meaningful pattern, e.g. data01.csv, data02.csv, ...You can adapt using your naming convention. You can do something like this:
% define some parameters
numFiles = 3; % just making up an example
baseName = 'data' % modify this according to your naming convention
% preallocate array to hold data
botData = zeros(4,numFiles);
topData = zeros(4,numFiles);
% loop through data
for k = 1:numFiles
% generate the current file name
% e.g. data02, data13,...
filename = [baseName,num2str(k,'%02d'),'.csv']; % modify this according to your naming convention
% read the selected data and save it an array with a column for each
% file
% note row and column ranges are zero referenced cell a1 is 0,0
botData(:,k) = csvread(filename,1,2,[1,2,4,2]);
topData(:,k) = csvread(filename,6,2,[6,2,9,2]);
end
% make single vector of data for bottom and top data
botData = botData(:); % reshapes columwise, could also use reshape(4*numFiles,1)
topData = topData(:);

5 Commenti

I understand how to do it for specific section (green blocks). But the data I need is in secreal sections, I am assuming that is what the brackets are intended for but do not recognized the format, or what the values in the example provided are supposed to represent.
In my example script I used csvread rather than xlsread because you referred to your files as "csv" files in your original post rather than xls or xlsx files. The arguments are a little different for csvread (which I used) and xlsread (which you show in your comment). For csvread the argument e.g [1,2,4,2] means to read from row 1 to row 4 (the first and second elements) just in column 2 (the second and fourth elements). Note that these are "zero referenced" so that the upper left corner of the spread sheet is row 0, column 0
Note that readmatrix is now preferred (R2020A) over both csvread and xlsread, but I wasn't sure if you had the most current version of MATLAB so I used the older functions.
Oh, I also see in your more recent screen clip that the ranges of interest are not from rows 2 to 4 and 6 to 9 as they appear in you original screen clip. They are instead rows 15 to 18 (zero referenced) and rows 20 to 23.
You can adapt your code accordingly. It is also fine to use xlsread if it is simpler for you as long as you have the data saved in Excel worksheets and not csv files
Sorry, I sometimes use the csv and xlsx interchagabley and originally thought my files we the csv's.
The areas of interest are actually all 16 of the cells shown in the previous screen shot. The green boxes are all I have been able to read out successfully read out so far. I have been trying to modify the code to select out the other cells.
I am getting closer to what I need the script to do. I think the trouble I am having is matching the format ot the hold array to the data I am trying to pick out.
Curent error is "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Sorry I have been away from the computer for awhile. If you haven't solved this yet I suggest that I think you will need to do the reads in two passes:
botData(1:4,k) = xlsread(filename,1,'C16:C19');
botData(5:8,k) = xlsread(filename,1,'G16:G19');
and similar for the top data.
By rather than using screen shots of your code, you can use the code button on the MATLAB answers menu bar and copy and paste your code in. That way other people can easily copy it back out.

Questa domanda è chiusa.

Prodotti

Release

R2018b

Richiesto:

il 6 Ott 2020

Chiuso:

il 20 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by