How do I point at certain columns in a .csv file and then run calculations on it?
4 views (last 30 days)
Show older comments
Hello all, I currently have the code below that runs calculations and exports a matrix for my .csv files. Right now this is assuming my data is a clean 3Xwhatever matrix in my .csv files. Within my .csv files I only want to look at data in columns starting at B10, C10, and D10 and then running ot the end of those columns. Can matlab point to certain data within a .csv? I am assumning I need to tweak the M = readmatrix(fullfile(fn(ii).folder,fn(ii).name)) line or M = readmatrix(fullfile(fn(ii).folder,fn(ii).name)) line.
Any help is much appreciated! Thank you.
% appropriate dir() call that returns info
% about the files you want to process:
fn = dir('*.csv'); % this call returns info about .csv files in the current directory;
% you may need to modify it to work for your file locations
% (see dir documentation)
% number of files:
N_files = numel(fn);
% pre-allocate results matrix (one row per file, 3 columns):
results = zeros(N_files,3);
% read and process each file:
for ii = 1:N
% read the file:
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name));
% process the file's data:
S = sum(abs(diff(M,1,1)),1);
% store the result:
results(ii,:) = S;
end
% write the results file (can be located anywhere):
writematrix(results,'results.csv')
3 Comments
Accepted Answer
Voss
on 1 Oct 2024
You can modify the code as follows (specifying 9 header lines, and using only columns 2-4 of the matrix read):
% appropriate dir() call that returns info
% about the files you want to process:
fn = dir('*.csv'); % this call returns info about .csv files in the current directory;
% you may need to modify it to work for your file locations
% (see dir documentation)
% number of files:
N_files = numel(fn);
% pre-allocate results matrix (one row per file, 3 columns):
results = zeros(N_files,3);
% read and process each file:
for ii = 1:N_files
% read the file starting from line 10:
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name),'NumHeaderLines',9);
% process columns 2-4 of the file's data:
S = sum(abs(diff(M(:,2:4),1,1)),1);
% store the result:
results(ii,:) = S;
end
% write the results file (can be located anywhere):
writematrix(results,'results.csv')
Also note that N had to be changed to N_files in the for line (that was a mistake in my original code, sorry about that).
3 Comments
More Answers (1)
dpb
on 1 Oct 2024
Edited: dpb
on 1 Oct 2024
" only want to look at data in columns starting at B10, C10, and D10 and then running ot the end of those columns. Can matlab point to certain data within a .csv? "
...
nHdr=9; % header lines to skip
C=["B";"D"]; % start, ending column (adjacent) --> "B:D" here...change to suit
range=join(C,":"); % build a range expression
for ii=1:numel(fn)
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name), ...
'NumHeaderLines',nHdr,'Range',range);
...
See the doc for readmatrix and friends; also look into detectImportOptions to fine tune import even further such as returning noncontiguous ranges.
One could make a couple other minor adjustments to the calculational code; @Voss left yours untouched other than to select the specific columns to operate over from the whole array whereas the above only returns the desired columns.
...
nFiles=numel(fn); % number files to read
nCols=numel(char(C(1)):char(C(2))); % number of columns to read (try it, you'll like it!!! :))
S=zeros(nFiles,nCols); % only have on total sum by nCols wide
for ii=1:nF
M = readmatrix(fullfile(fn(ii).folder,fn(ii).name), ...
'NumHeaderLines',nHdr,'Range',range);
S(ii,:)=sum(abs(diff(M,1,1)));
end
writematrix(S, ...) % save results as desired...
@Voss is correct if there were only one row; was not a case I figured would occur, but code should account for. agreed...I initially was thinking it was only the overall summation that was wanted, not the individual sums...looking back at the original I see I had misread it...
3 Comments
dpb
on 1 Oct 2024
Edited: dpb
on 1 Oct 2024
I mixed old and new code and the Submit button quit working so I went away for awhile, @Voss.
I thought originall the overall total was all that was wanted and was going to compute it that needs only a single row for output but rereading I see I missed there.
Agreed; I didn't consider the possibility of there being only a single row but the code should handle just in case.
It's interesting that
M = [8 -4 10];
abs(diff(M,1,1))
is empty but
sum(abs(diff(M,1,1)))
gets converted to the zeros vector. I don't know that I had ever noticed that behavior before.
It did let me correct now. I've been having lots of trouble with the forum with latest release of Firefox over last couple of weeks or so -- it brings up the window but have to manually refresh multiple times or close and try again before can get access to do anything....don't know if TMW changed something or Firefox introduced a bug...
See Also
Categories
Find more on Naming Conventions 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!