Check if multiple files exist in the directory - matlab

How can I check if there are several CSV files in the current directory?

I have a csv file called PowerOutput.csv, I see if this exists with

exist('PowerOutput.csv','file') 

However, I could have several such files, for example. PowerOutput1.csv, PowerOutput2.csv, PowerOutput3.csv, etc.

What is the best way to find files in a directory?

At the moment, I have tried:

 TopFolder = pwd; SubFolder = dir(TopFolder); SubFolder = {SubFolder.name}; SubFolder(strncmp(SubFolder,'.',1)) = []; % -- find the number of PowerOutput num_Power = strncmp({'PowerOutput'}, SubFolder,length('PowerOutput')); num_Power(num_Power == 0) = []; num_Power = 1:length(num_Power); 

and then I can import the data:

 % -- import inflow for i = 1:length(num_Power); filename = fullfile(TopFolder,horzcat('PowerOutput',num2str(num_Power(i)),'.csv')); fid = fopen(filename); headers = textscan(fid, '%s%s', 1, 'delimiter',','); dat = textscan(fid,'%s%f%f','delimiter',',','headerlines',1); fclose(fid); end 

But this seems like a very long way to do it. Any suggestions?

+4
source share
1 answer

use * in dir :

 files = dir( fullfile( TopFolder, SubFolder.name, 'PowerOutput*.cvs' ) ); 
+5
source

Source: https://habr.com/ru/post/1484193/


All Articles