Appending dataset of varying length
Show older comments
I have a collection of monthly 1 minute averaged data files that I would like to import, append, and process. I would like to handle the data in Matlab using a dataset array:
wind = dataset('file','Halkirk1_12_2010_average1min.csv','delimiter',',','format',['%s' repmat(' %f',1,72)]);
Due to the nature of the files, they are varying lengths so vertcat does not work. I will use datevec to pick up the next file to append. Is there a function or method that will append two datasets of varying lengths? Any tips or thoughts would be appreciated.
2 Comments
Oleg Komarov
on 17 Apr 2011
You mean you have different number of columns?
Braden
on 18 Apr 2011
Accepted Answer
More Answers (2)
Laura Proctor
on 18 Apr 2011
0 votes
4 Comments
Braden
on 18 Apr 2011
Laura Proctor
on 19 Apr 2011
A full outer join will return the union of observations from both datasets.
c = join(a,b,'Type','outer')
Oleg Komarov
on 19 Apr 2011
@Laura: which is not what the op wants if she wants to append.
Braden
on 19 Apr 2011
Richard Willey
on 19 Apr 2011
Hi Oleg
This strikes me as more of a data representation issue than a question of MATLAB syntax. Your eventual solution will depend on how you want to describe "time". You seem to be assuming a "wide" format in which the observations for each month are stored as separate variables and each row represents a separate one minute average (the first one minute average in the month, the second one minute average in the month, ...) This format will work fine, however, you might need to use some NaNs to pad out some of the monthes.
You might find it easier if you created a variable labeled "Time" and used this to measure all of your observations. You could create separate variables that track what month this time value corresponds to, what day of the week it is, whether its a holiday, what have you.
I'm attaching some code that I wrote a while back that grabs data from xls files and automatically creates nominal variables based on the file name.
Hope that this proves helpful
%%Loading Data into MATLAB
clear all
clc
% This script assumes that we have a set of XLS files.
% Each XLS file contains a separate spark sweep
% We're interested in combining all these files into a dataset array
% After which, we're going to identify the minimum BSFC for each spark
% sweep
%Identify where to search for files
Location = 'H:\Documents\MATLAB\BSFC\';
% Store the name of all .xls files as a vector D
D = dir([Location, '*.xls']);
% Create a dataset array from the file that is the first element in D
name = D(1) .name
Engine = dataset('xlsfile',name);
% Use the name of the file as a nominal variable
% The nominal variable can be used to note that all these rows came from
% the file with name = "name"
% Start by stripping off the ".xls" extension
name = name(1:end-4);
% Write the name to the dataset array and convert to a nominal
Engine.Name = repmat(name,length(Engine),1);
Engine.Name = nominal(Engine.Name);
% Repeat for all the rest of the .xls files in the "Location".
% Each new file with be vertically concatenated with the
% original dataset array
f = @(x,y) vertcat(x,y);
parfor i = 2 : length(D)
name = D(i) .name
Engine2 = dataset('xlsfile',name);
name = name(1:end-4);
Engine2.Name = repmat(name,length(Engine2),1);
Engine2.Name = nominal(Engine2.Name);
Engine = f(Engine, Engine2);
end
10 Comments
Braden
on 19 Apr 2011
Braden
on 27 Apr 2011
Oleg Komarov
on 28 Apr 2011
As it says it's unable to open the file Halkirk1_10_2010_average10min.csv
Is the file there? Is the name correct? Try to fopen('...\Halkirk1_10_2010_average10min.csv') and see what it returns (doc fopen).
Braden
on 2 May 2011
Oleg Komarov
on 3 May 2011
What if you try to use alone:
wind2 = dataset('file',name,'delimiter',',','format',['%s' repmat(' %f',1,72)]);
Or if you try to use a simple for instead of parfor.
Braden
on 3 May 2011
Oleg Komarov
on 3 May 2011
That's very weird. Datasets do add overhead but even a cell-array:
C(1:32000,1:75) = deal({'qwertyuiop'});
is just ~180 mb.
Braden
on 6 May 2011
Teja Muppirala
on 6 May 2011
A very common mistake.
zeros(size(wind,1)) <--- Out of memory. This is not what you meant.
zeros(size(wind,1),1) <--- This is what you meant to write
Fix all of those lines to be:
wind.vhub = zeros(size(wind,1),1);
wind.newWinV = zeros(size(wind,1),1);
wind.newWinVMax = zeros(size(wind,1),1);
wind.newWinVMin = zeros(size(wind,1),1);
wind.newSonWinV = zeros(size(wind,1),1);
wind.newSonWinVMax = zeros(size(wind,1),1);
wind.newSonWinVMin = zeros(size(wind,1),1);
wind.phub = zeros(size(wind,1),1);
wind.rho = zeros(size(wind,1),1);
wind.Cp = zeros(size(wind,1),1);
wind.normpow = zeros(size(wind,1),1);
Braden
on 6 May 2011
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!