For Each
Sean's pick this week is For-Each by Jeremy Hughes.
My pick this week is a toolbox from our development organization for a for-each loop construct in MATLAB. A for-each loop allows you to iterate over a set or dimension without having to worry about indexing.
MATLAB actually already has this built in for single elements of numeric arrays:
% Display each element in x for thisfile = [1 pi 8 -3] disp(thisfile) end
1 3.1416 8 -3
This could've course been written with an index like below.
% Display each element in x with an index thisfile = [1 pi 8 -3]; for ii = 1:numel(thisfile) disp(thisfile(ii)) end
1 3.1416 8 -3
But what if we want to traverse every column or slice of a three dimensional array? Or each element in a cell array? Or each combination of two arrays? This is where Jeremy's for-each construct comes in handy since you won't need to worry about indexing.
Let's take a bunch of csv files and make copies of them with just the rows of data we care about. These files contain energy outage information for energy outages in America. Being from New England, I just want the records that correspond to the North East.
% Scrape the data directory for all csv files datadir = '.\Data\'; files = dir([datadir '*.csv']); filenames = {files.name}; disp(filenames)
Columns 1 through 4 '2002Data.csv' '2003Data.csv' '2004Data.csv' '2005Data.csv' Columns 5 through 8 '2006Data.csv' '2007Data.csv' '2008Data.csv' '2009Data.csv' Columns 9 through 12 '2010Data.csv' '2011Data.csv' '2012Data.csv' '2013Data.csv'
% Make a directory for output outputdir = 'NorthEastData'; mkdir(outputdir) % Loop over each filename, read, extract, write for thisfile = each(filenames) % Reach each file Temp = readtable([datadir thisfile]); Temp.Region = categorical(Temp.Region); % Convert to categorical % Identify and extract the northeast records northeast = Temp.Region == 'NorthEast'; Output = Temp(northeast,:); % Make new full file name [~,name,ext] = fileparts(thisfile); outputfile = fullfile(pwd,outputdir,[name '_NorthEast' ext]); % Write out writetable(Output,outputfile) end
Now we can check to make sure it did what we expect.
Personally, I see this being really useful for working with sets of files or images, like I have done above, or for needle in the haystack type problems where I want to find something in a bunch of combinations and stop as soon as I do.
Advanced Programming Challenge!
And now for a challenge:
I used the informal interface to this toolbox, i.e. the each function, to traverse each file. There is also a formal interface that provides the ability to define your own iterators so that you can traverse objects of your own class or anything else. I will give some MathWorks goodies to anyone who will write their own eachFile iterator that I could use above to recreate what I have.
Please post it below.
Comments
The development team is actively looking for feedback on this, so please leave them a comment with any thoughts you have.
Give it a try and let us know what you think here.
- カテゴリ:
- Picks
コメント
コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。