how to import multiple csv file in matlab?

i have 500 csv file that i need to e import in matlab ....and i need only 2 column of each file and dont want that first 2 row....and i saw this but not working...for example consider that m files location are at 'D:\core'

Answers (2)

Jon
Jon on 28 Nov 2023
Use the dir command to get a list of files to loop through, You can search for example for '*.csv"
Depending what you want to do with the values you can use readmatrix or readtable in a loop to import them. Use the numHeaderLines parameter to skip the first two lines, use the Range parameter to just get the first two columns
Type doc dir, and doc readmatrix on the command line to get all of the documentation details

11 Comments

can u write that down in an example cause i tried it and not working...
clc
clear all
% global CH1 CH2
files = dir('*. csv')
for i=1:length(files)
data = readmatrix(files(i).name); % or csvread(files(i).name)
% process data
writematrix(processed_data,txt_filename); % save to txt file
end
% a=csvread('a1.csv',2);
V=a(:,2)';
I=a(:,3)'/(56);
%I=I*1000;
k=1;
VVV=V(104:131)*1000/6;
E=VVV/30;
III=I(104:131);
[m,n]=size(III);
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
plot(E,sig);
"can u write that down in an example cause i tried it and not working..."
How exactly is it not working? Please specify.
What is the code after the 1st for loop supposed to do?
did u even read my question?????
Yes, I did. And then I looked at your comment above.
Seeing the code, I am asking follow-up questions about it.
You said, you tried something and it didn't work. I asked how does it not work. Do you get an error? Do you not get the expected output?
no i didnt get anything at all....
V=a(:,2)';
I=a(:,3)'/(56);
these lines select second and third column of those files...
and for these lines
files = dir('*. csv')
for i=1:length(files)
data = readmatrix(files(i).name); % or csvread(files(i).name)
% process data
writematrix(processed_data,txt_filename); % save to txt file
end
i get
If files is empty (0 x 1) structure, then you probably are not running the script in the directory where the .csv files are stored. Either move your current matlab directory to the location where the files are stored, or use a more complete path to point to where the files are stored, e.g.
files = dir('d:\core\*.csv')
Regarding your earlier comment to @Dyuman Joshi "did u even read my question?????", I want to politely let you know that this type of response is completely inappropriate on this site. People are volunteering their personal time to help you with your problems so that you can become a better MATLAB coder. This is a very friendly site, no need for what, at least to me, sounds like angry replies.
You have a lot of other problems with your code too, but it is hard to know where to start. At the least, any operations you do on the individual matrices after you read them, should be performed inside of the same loop. Otherwise, unless you store the data somewhere it will just get overwritten.
Also, as I pointed out in my original post, you should include the numHeaderLines, name/value argument to skip the first two rows, for example, building on your code:
data = readmatrix(files(i).name,'numHeaderLines',2)
sorry for my behavior but i explained everything so it is obvious to me that whats code doing...and thank u for ur time...anyway...where and whats wrong with my code except first lines....cause u said"You have a lot of other problems with your code too"
Ooops, meant to put this in my thread, put in @Image Analyst by accident
Here is an example script to get you started. You will have to modify further according to the specifics of your project
% Get a list of the available files to be read in
% (I have your test files in 'c:\temp\matlab\core', you would replace the
% search path in the command below with the location you have your files
% in)
datapath = 'c:\temp\matlab\core'; % directory where data files are stored
files = dir(fullfile(datapath,'*.csv'));
% Loop through the files, reading them in, processing them, and then saving
% the output
for k = 1:numel(files) % I use k as my loop index, i is usually reserved for complex values
% Read in the file, skipping the header lines
% Construct the absolute path to the files, since we might not be in
% that directory
filename = fullfile(files(k).folder,files(k).name);
data = readmatrix(filename,"NumHeaderLines",2);
% Assign values using only second and third columns of data matrix
% (just trying to do something like you show in your code)
V = data(:,2);
I = data(:,3);
% Do something with these values (just an example, you can put in your
% processing here)
P = I.*V;
% Write the data to an output file,
% (not sure what you wanted to do here, but it looked like you
% wanted to make an output file for each input data set, because you
% had the writematrix statement inside of the same loop)
% Here I name the files % out001.csv,out002.csv,... you can modify
% according to your needs
outfile = fullfile(datapath,['out',num2str(k,'%03d'),'.csv']);
writematrix(P,outfile)
end
thanks to u sir, i can read all my files but i modified according to what i needed...it only gives me 1 output...and E data is all same that shouldnt be
clc
clear all
datapath = 'I:\A';
files = dir(fullfile(datapath,'*.csv'));
for k = 1:numel(files)
filename = fullfile(files(k).folder,files(k).name);
data = readmatrix(filename,"NumHeaderLines",2);
V = data(:,2);
I = data(:,3)/(56);
VVV=V(104:131)*1000/6;
E=VVV/30;
III=I(104:131);
[m,n]=size(III);
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
for i=1:n
P=zeros(m,2);
P(:,1)=E;
P(:,2)=sig(i);
plot(E,sig);
hold on;
end
outfile = fullfile(datapath,['out',num2str(k,'%03d'),'.csv']);
writematrix(P,outfile)
end
So did you still have any questions, or are you able to move ahead now? If this answered your question please "accept" the answer so that others with similar issues will know that an answer is available
Looking a little more at your code, here are some issues that I see
  • In your first loop, you increment a counter, k, but you don't do anything with it. Not sure what you intended there, but it doesn't look right
  • In your second loop, You keep overwriting the values of P with each loop iteration, If you intended for example to have each row of P calculated as you looped, then you should include the loop index in the left hand side of your expression, so something perhaps like this.
P(i,1) = E(i);
P(i,2) = sig(i);
Also, you should remove f the assignment inside of the loop
P = zeros(m,2)
Since m doesn't change inside of the loop, this just keeps assigning the second column of the last row of P to zero, I don't think this is what you intended.

Sign in to comment.

If you still can't figure it out, attach 2 or 3 of the files and write back.

2 Comments

here is 3 files...and agai i tried @Jon way but nothing
Try this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Specify the folder where the files live.
dataFolder = pwd; %'C:\Users\yourUserName\Documents\My Data';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(dataFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', dataFolder);
uiwait(warndlg(errorMessage));
dataFolder = uigetdir(); % Ask for a new one.
if dataFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(dataFolder, 'a*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf('Now reading file %d of %d : "%s"\n', k, length(theFiles), fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an array with readmatrix.
data = readmatrix(fullFileName, 'NumHeaderLines', 3);
[rows, columns] = size(data)
if columns < 3
continue;
end
% Your code. Not sure what it all does because you didn't comment it.
x = data(:, 1);
V = data(:, 2);
I = data(:, 3) / 56;
VVV = V(104:131)*1000/6;
E = VVV/30;
III = I(104:131);
[m, n] = size(III);
k=1;
for i=1:n
sig(i)=(III(i)*1000/(4*pi))/(VVV(i)/0.3);
k=k+1;
end
for i=1:n
P=zeros(m,2);
P(:,1)=E;
P(:,2)=sig(i);
plot(E,sig);
hold on;
end
% Write output to a table.
baseOutputFileName = sprintf('out%3.3d.csv', k);
outputFullFileName = fullfile(dataFolder, baseOutputFileName);
fprintf('Now writing output file %d of %d : "%s"\n', k, length(theFiles), outputFullFileName);
writematrix(P, outputFullFileName)
% ch1 = data(:, 2);
% ch2 = data(:, 3);
% nexttile
% plot(x, ch1, 'b-');
xlabel('X', 'FontSize',fontSize)
ylabel('Volts', 'FontSize',fontSize)
grid on;
% hold on;
% plot(x, ch2, 'r-');
title(baseFileName, 'FontSize',fontSize, 'Interpreter','none')
legend('ch1', 'ch2')
drawnow; % Force display to update immediately.
end
Not sure what all your code does with extracting, computing, plotting, etc. but at least this does read in the data.

Sign in to comment.

Categories

Products

Release

R2016b

Asked:

on 28 Nov 2023

Commented:

on 6 Dec 2023

Community Treasure Hunt

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

Start Hunting!