Effacer les filtres
Effacer les filtres

How do I loop this code to open each file, conduct a simple operation and resave?

1 vue (au cours des 30 derniers jours)
Laurentiu Galan
Laurentiu Galan le 13 Nov 2011
Hi,
I have a bunch .csv with five columns. I am trying to create a loop that will open each file in the directory, take each value in column 5 and add one to it and place it in a new column (column 6) and overwrite the previous file (but not delete the existing values)
I have the following code, but how do I open each one sequentially?
%Matlab code to create matrix for files
Files = dir('c:\Users\Laurentiu Galan\Desktop\tickoutput\*.csv');
fnames = {Files.name};
filename = fnames';
%Determine Size for Loop
D = size(filename);
Numloop = D(1,1);
%Loop to Create Paths for each file
for i=1:Numloop
thispath(i,1) = strcat('c:\Users\Laurentiu
Galan\Desktop\tickoutput\', filename(i,1));
end
%Loop to open each file and add one to each value in a new column 'column 6'????
???
Thanks, I know its a nested if then statement, any ideas?

Réponses (2)

Fangjun Jiang
Fangjun Jiang le 13 Nov 2011
Start with this and then do the data processing.
PathStr('c:\Users\Laurentiu Galan\Desktop\tickoutput');
Files = dir(fullfile(PathStr,'*.csv'));
for k=1:length(Files)
Data=importdata(fullfile(PathStr,Files(k).name));
end
  3 commentaires
Laurentiu Galan
Laurentiu Galan le 13 Nov 2011
Any ideas how not to do it with the directory?
Walter Roberson
Walter Roberson le 13 Nov 2011
You cannot do it without using dir()
Your code shown does not read in any data, and so is not functional as is.
MATLAB will not inherently crash for importing 23000 files.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 13 Nov 2011
Very simple. Use csvread() to read it into an array, insert your new column, use csvwrite() to write out the new array. Follow the examples in the FAQ for reading and writing a bunch of files in a loop:

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by