Borrar filtros
Borrar filtros

How do I read csv files with common characters in filename

13 visualizaciones (últimos 30 días)
Can anyone please tell me how I write a script or function that combines files, where the first 6 (could be as little as 5 and no more than 10) characters of the filename are the same, into one variable? In MSDOS there is a wild card which is * and a question mark replaces the characters so if I wanted to call on the files that start the same I'd do ??????*.csv but I cannot see how to do this in Matlab.
I did an experiment using a load cell and the data logger split the recordings for the same set because the files were too big (it said). At the moment I'm manually changing xlsread() for each file and I have hundreds of files so having a script that would call on the files that go together would be amazing. The filenames all vary slightly with .1 added at the end for each extra file from the same dataset. I had to do this to batch edit them all to .csv as Matlab would not read them as .xlsm and .xlsx.
so far I have this
clc
[num, others] = xlsread('C:\Users\User\Documents\MATLAB\Anemones\F0_ Anemones001.1.csv');%import first file, may be up to three per reading
Volts1 = xlsread('C:\Users\User\Documents\MATLAB\Anemones\F0_ Anemones001.1.csv'); %renames to Volts1
Volts1 = num(:,3); %selects third column only
clear num;
clear others;
Volts1 = Volts1(Volts1~=0); %Checks for zeros and deletes
[num, others] = xlsread('C:\Users\User\Documents\MATLAB\Anemones\F0_ Anemones001.csv'); %Gets second lot for dataset, max three per set
Volts2 = xlsread('C:\Users\User\Documents\MATLAB\Anemones\F0_ Anemones001.1.csv'); %renames to Volts2
Volts2 = num(:,3); %selects third column only
clear num;
clear others;
Volts2 = Volts2(Volts2~=0); %Checks for zeros and deletes
Volts = [Volts1;Volts2]; %combines vectors into one array
clear Volts1;
clear Volts2;
Your help would be much appreciated. I know about strcat() but not how to select the files. There are up to three files per dataset.

Respuesta aceptada

Sachin Shrestha
Sachin Shrestha el 9 de Jun. de 2016
Hello Vitkoi,
The following lines of code should do the trick for you.
sourceDir = 'D:\OneDrive\projects\Mathworks-Profile\csv files'; % location of your csv files
sourceFiles = dir(fullfile(sourceDir, '*.csv')); % filters for the csv files
characters2Check = 'SampleCSV1_'; % the characters of the file name to be checked
for i=1:numel(sourceFiles) % loop around for all the csv files of folder
sourceFileName = sourceFiles(i).name; % extracts the name of the csv file
if(strcmp(sourceFileName(1:length(characters2Check)),characters2Check)) % checks the presence of characters
fullName=[sourceDir '\' sourceFileName]; % define path and filename
[num,others] = xlsread(fullName);
% --- do whatever you want to do with obtained values
% ---
end
end
Hope this will help. Good luck!
  1 comentario
Viktoi R
Viktoi R el 10 de Jun. de 2016
That is a great help, thank you. It also helps so much to have the annotations. I have seen so many answers on the Matlab page that answer the question asked perfectly but without the annotation I have been unable to work out how to edit the code to use it for myself. Your time is very much appreciated.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by