check if a number of files exists in a directory - matlab - file

How could I check if a number of csv files are present in the current directory?
I have a csv file called PowerOutput.csv, I can see if this exists with
exist('PowerOutput.csv','file')
However, I could have a number of these files e.g. PowerOutput1.csv, PowerOutput2.csv, PowerOutput3.csv and so on.
What is the best way of finding which files exist 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 by:
% -- 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 really long-winded way of doing it. Any suggestions?

use * in dir:
files = dir( fullfile( TopFolder, SubFolder.name, 'PowerOutput*.cvs' ) );

Related

How to make an array of .mat files and append it with a for

I have that code,
h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)'); %angry
h2 = dir('C:\Users\John\Documents\MATLAB\code for yannis\neutral(N)');%neutral
h3 = dir('C:\Users\John\Documents\MATLAB\code for yannis\happiness(F)');%happy
%fprintf('%s', filename);%filename h(i,1).name
fprintf('%d \n',numel(h1));
fprintf('%d \n',numel(h2));
fprintf('%d \n',numel(h3));
%fprintf('%d', max(numel(h1),numel(h2),numel(h3)));
A= [numel(h1) numel(h2) numel(h3)];
fprintf('%d \n \n \n', max(A));
fprintf('%s \n', h1(2).name);
%load('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\*.mat');
fprintf('%d \n \n \n', length(h1));
resultsdir = 'results';
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
array = [h1(2)];
for i=3:max(A)+3
%s= 'C:\Users\John\Documents\MATLAB\code for yannis\anger(W)';
thisfile = h1(i).name;
destfile = fullfile(resultsdir, thisfile);
thisdata = load(thisfile);
%array[thisdata];
%strcmp(h(i,1).name(1:2), s);
%cat(1, array, h1(i,1).M);
fprintf('%s \n', h1(i,1).name);
end
and i want to keep all the .mat files in an array in order to compare it with other mat files
Thanks in advance
You can select the top level folder and utilize your system's dir call to recursively search for all files that match your criteria (*.mat, in this case)
For example:
mypath = uigetdir('', 'Select Top Level Folder');
oldpath = cd(mypath); % cd to data directory for simpler dir call
[~, cmdout] = system('dir /S /B *.mat');
cd(oldpath); % Return to previous path
mymatfiles = regexp(cmdout, '(.:\\[\w\-\\. ]+\.\w+)', 'match');
The system call returns one long string with all of the absolute paths to your *.mat files. I utilize a regexp to split this into a cell array, where each cell is an absolute path to a single *.mat file.
Note that this is Windows only because it uses the MS-DOS dir command. This function can easily be adapted to other operating systems, I just don't know them.
Assuming you have .mat files in all of those directories (ending in ".mat"), simply get the directory listings of each of those directories and store them.
% All of the folders where the *.mat files live
folders = {'C:\folder1'; 'C:\folder2'};
allfiles = cell();
for k = 1:numel(folders)
% Find all the .mat files
files = dir(fullfile(folders{k}, '*.mat'));
% Convert to absolute paths
fullpaths = cellfun(#(x)fullfile(folders{k}, x), {files.name}, 'uniform', 0);
% Store in our global cell array
allfiles = cat(1, allfiles, fullpaths(:));
end
Now allfiles contains an entry for every .mat file contained within those paths. You can then loop through this cell array to perform any operations you need.
alldata = cellfun(#load, allfiles, 'uniform', 0);

Find string in log files and return extra characters

How can I get Python to loop through a directory and find a specific string in each file located within that directory, then output a summary of what it found?
I want to search the long files for the following string:
FIRMWARE_VERSION = "2.15"
Only, the firmware version can be different in each file. So I want the log file to report back with whatever version it finds.
import glob
import os
print("The following list contains the firmware version of each server.\n")
os.chdir( "LOGS\\" )
for file in glob.glob('*.log'):
with open(file) as f:
contents = f.read()
if 'FIRMWARE_VERSION = "' in contents:
print (file + " = ???)
I was thinking I could use something like the following to return the extra characters but it's not working.
file[:+5]
I want the output to look something like this:
server1.web.com = FIRMWARE_VERSION = "2.16"
server2.web.com = FIRMWARE_VERSION = "3.01"
server3.web.com = FIRMWARE_VERSION = "1.26"
server4.web.com = FIRMWARE_VERSION = "4.1"
server5.web.com = FIRMWARE_VERSION = "3.50"
Any suggestions on how I can do this?
You can use regex for grub the text :
import re
for file in glob.glob('*.log'):
with open(file) as f:
contents = f.read()
if 'FIRMWARE_VERSION = "' in contents:
print (file + '='+ re.search(r'FIRMWARE_VERSION ="([\d.]+)"',contents).group(1))
In this case re.search will do the job! with searching the file content based on the following pattern :
r'FIRMWARE_VERSION ="([\d.]+)"'
that find a float number between two double quote!also you can use the following that match anything right after FIRMWARE_VERSIONbetween two double quote.
r'FIRMWARE_VERSION =(".*")'

How to exclude the zeros that results from file's self comparisons and keep all the values other than that in the final array

i want to exclude the zeros that results from file's self comparisons and keep all the values other than that in the final array. what i am doing is using ~= for file comparison but not succeeded yet. Also tried strcmp. my code is as follows.
dirName= 'C:\Users\PCPC\U1'; %# folder path
files = dir( fullfile(dirName,'*.HWR') ); %# list all *.HWR files
files = {files.name}; %'# file names
supfinal = [];
final = [];
dist = [];
for i=1:length(files)
fname = fullfile(dirName,files{i});
c = dlmread(fname, '', 0,0);
c1=c(:,1);
for j=1:length(files)
fname = fullfile(dirName,files{j});
d = dlmread(fname, '', 0,0);
d1=d(:,1);
**if (j ~= i)**
dist = dtw(c1,d1); %# SOme Calculation
final = [final; dist];
end
end
**supfinal = [supfinal; final];**
end
i want from my code to give me results other than self comparison in the supfinal array. what's wrong i am doing. How to correct?

Looping through A Folder and all its subdirectories

Ok. Many trouble shooting hours ...and many error "dings" later, I'm still having the same problem. Due to my beginner skills I'm having problems achieving the following segment of my project:
I will be as detailed as possible so I can hopefully nail it this time:
On my computer i have a folder C:\data which contains many different subfolders.
The subfolders are named by dates in a MMDDYY fashion. For example "040312"
In each subfolder are excel files named after Baseball teams. each subfolder may contain a different combination of xls files.
I am trying to write code that achieves the following objectives:
1.) Loops through all the subfolders of the C:\data folder looking for xls files that have the filenames: Angles.xls, Diamondbacks.xls, etc.
2.) If the files are found in each subfolder import the spreadsheet data and generate a plot of the data titled "Score" and "Allow".
3.) If the file is not found any given subfolder skip and continue to the next file to be located.
4.)Then save the generated plot in the same folder that the spreadsheet was imported from as a .fig and a .bmp file.
I've gotten hints to use various functions like: genpath, dir, but the code I've been fumbling through isn't able to achieve my goals.
a) the script doesn't import the excel files from all the subfolders
b) the script wont save the .fig or .bmp file in the associated subfolder
Here is the code I have been fumbling through:
%I know all of this is wrong wrong wrong. Please help to adjust my code to %achieve the objectives outlined above!
addpath(genpath('c:\data'))
folder = 'c:\data';
subdirs = dir(folder);
subdirs(~[subdirs.isdir]) = [] ;
numberOfFolders = length(subdirs);
if numberOfFolders <= 0
uiwait(warndlg('Number of folders = 0!'))
end
wantedfiles = {'Angels' 'Diamondbacks' 'Orioles' 'Royals' 'Yankees' 'Mets' 'Giants'};
for K = 1 : numberOfFolders
thissubdir = subdirs(K).name;
if strcmp(thissubdir, '.') || strcmp(thissubdir, '..')
continue;
end
subdirpath = [folder '\' thissubdir];
for L = 1 : length(wantedfiles)
for wantedfiles = {'Angels' 'Diamondbacks' 'Orioles' 'Royals' 'Yankees' 'Mets' 'Giants'};
folder = '';
fileToRead1 = [wantedfiles{1} '.xls'];
sheetName='Sheet1';
if exist(fileToRead1, 'file') == 0
% File does not exist
% Skip to bottom of loop and continue with the loop
continue;
end
%This is to import the data and organize it
% All of this code I had auto-generated from importing files manually
[numbers, strings, raw] = xlsread(fileToRead1, sheetName);
if ~isempty(numbers)
newData1.data = numbers;
end
if ~isempty(strings) && ~isempty(numbers)
[strRows, strCols] = size(strings);
[numRows, numCols] = size(numbers);
likelyRow = size(raw,1) - numRows;
% Break the data up into a new structure with one field per column.
if strCols == numCols && likelyRow > 0 && strRows >= likelyRow
newData1.colheaders = strings(likelyRow, :);
end
end
% Create new variables in the base workspace from those fields.
for i = 1:size(newData1.colheaders, 2)
assignin('base', genvarname(newData1.colheaders{i}), newData1.data(:,i));
end
% Now I execute the plotting of data
subplot (2,1,1), plot(Score,Allow)
title([wantedfiles{1} 'Testing to see if it works']);
subplot (2,1,2), plot(Allow,Score)
title('Well, did it?');
% here I save the generated plots, but they don't save where I want them to
saveas(gcf,[wantedfiles{1} ' did it work.fig']);
saveas(gcf,[wantedfiles{1} ' did it work.bmp']);
end
end
end
%At the end of the script I still was unable to loop over the files that I wanted
rmpath(genpath('c:\data'));

MATLAB: import all files from multiple directories and create a single column vector of 2 of the variables

I am a beginner in MATLAB coding so I have taken code from else-ware to apply it to my own needs. I've so far managed to get all files from one folder into a column vector, but now I want to get all files from all folders within a parent directory into this single column vector.
here's my code:
...
folder = ('parent_directory_path_name\01');
files = eval(['dir(''' folder '\*wind*.na'')']); % take files with wind in name
N = length(files);
%%
for n=1:N
filename = files(n).name;
eval(['fid = fopen(''' folder '/' filename ''');'])
data=textscan(fid, '%s','delimiter','\n');
lines=data{1};
lines=lines(56:end);
for i=1:size(lines,1)
[s(i).time s(i).east s(i).north] = strread(lines{i},'%f %f %f %*f %*f %*f %*f %*f');
end
time = [s.time]';
east (:,n) = [s.east]';
north(:,n) = [s.north]';
fclose(fid);
end
%%
ea = east (:); % put matrix columns into 1 column
no = north (:);
...
I'm sure there must be a simple loop I can put around the folder specification, but I can't work it out. I've also looked at:
How to get all files under a specific directory in MATLAB? , but as the method is very different I'm unsure how to apply this to the code I'm using.
Any tips would be much appreciated.
Thanks,
Luke
EDIT
Re:Shai, (comments section not big enough)
Okay, I've given it a try but I don't get how to open the files now. Here's what I've tried:
sub_f = dir( fullfile( 'parent_folder', '*' ) );
for si = 1:numel( sub_f )
if sub_f(si).name(1) =='.', continue;
end; % skip '.' and '..'
files = dir( fullfile( 'parent_folder', sub_f(si).name, '*wind*.na' ) ); % get all files in sub folder
for n = 1:numel(files)
% put your code here...
filename = files(n).name;
fid = fopen('' sub_f '/' filename '');
data=textscan(fid, '%s','delimiter','\n');
lines=data{1};
lines=lines(56:end);
for i=1:size(lines,1)
[s(i).time s(i).east s(i).north] = strread(lines{i},'%f %f %f %*f %*f %*f %*f %*f');
end
time = [s.time]';
east (:,n) = [s.east]';
north(:,n) = [s.north]';
fclose(fid);
end
end
Thanks again!
A few comments:
instead of concatenating folder names and file names as simple strings, a better practice would be to use fullfile command:
folder = fullfile( 'parent_folder', '01' );
You do not need the cumbersome evel expressions:
files = dir( fullfile( folder, '*wind*.na' ) );
For iterating over sub folders you can simply do
sub_f = dir( fullfile( 'parent_folder', '*' ) );
for si = 1:numel( sub_f )
if sub_f(si).name(1) =='.', continue; end; % skip '.' and '..'
files = dir( fullfile( 'parent_folder', sub_f(si).name, '*wind*.na' ) ); get all files in sub folder
for n = 1:numel(files)
% put your code here...
end
end

Resources