how to import multiple csv file in matlab?

109 visualizaciones (últimos 30 días)
arian hoseini
arian hoseini el 28 de Nov. de 2023
Comentada: Image Analyst el 6 de Dic. de 2023
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'

Respuestas (2)

Jon
Jon el 28 de Nov. de 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 comentarios
Jon
Jon el 6 de Dic. de 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
Jon el 6 de Dic. de 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.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 29 de Nov. de 2023
If you still can't figure it out, attach 2 or 3 of the files and write back.
  2 comentarios
arian hoseini
arian hoseini el 29 de Nov. de 2023
Editada: arian hoseini el 5 de Dic. de 2023
here is 3 files...and agai i tried @Jon way but nothing
Image Analyst
Image Analyst el 6 de Dic. de 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.

Iniciar sesión para comentar.

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by