How do I read csv files with common characters in filename
12 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Sachin Shrestha
2016 年 6 月 9 日
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!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!