how to import multiple csv file in matlab?
Show older comments
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
on 28 Nov 2023
1 vote
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
arian hoseini
on 28 Nov 2023
Dyuman Joshi
on 28 Nov 2023
"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?
arian hoseini
on 28 Nov 2023
Dyuman Joshi
on 28 Nov 2023
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?
arian hoseini
on 28 Nov 2023
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)
arian hoseini
on 29 Nov 2023
Jon
on 29 Nov 2023
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
arian hoseini
on 5 Dec 2023
Jon
on 6 Dec 2023
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
Jon
on 6 Dec 2023
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.
Image Analyst
on 29 Nov 2023
0 votes
If you still can't figure it out, attach 2 or 3 of the files and write back.
2 Comments
arian hoseini
on 29 Nov 2023
Edited: arian hoseini
on 5 Dec 2023
Image Analyst
on 6 Dec 2023
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.
Categories
Find more on Data Import and Export 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!