Question

I have a cell array with 22 columns. I want to read through the cell array and separate it into different .mat files based on column 2 (the site in string format). Basically, the data has information from one year for sites all over New York. I want to save the data for each site separately (find the rows with the same column 2 and save them).

I'd also like to convert the .mat file to a netcdf file so people who don't use MATLAB can also read it, but first, I just need to be able to separate the cell array without manually finding each specific string and saving it.

The data is this file: https://www.dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo

I use this script to read in the file and then sort it by the date (column 1):

filename = ('PM2.5_NY_2012.csv'); % PM2.5 88101 data from NY in the year 2012

% Use functions created by read_mixed_csv.m to read in
data = read_mixed_csv(filename,','); % Creates cell array of data
data = regexprep(data, '^"|"$',''); % Gets rid of double quotes at the start and end of the string 

% Sort data based on date (Column 1)
[Y,I] = sort(data(:,1)); % Create 1st column sorted
site_sorted = data(I,:); % Sort the entire array

So now it's a cell array. How can I save all the data with the same 2nd column into a different file? Would using 'unique' or 'find' be useful? I know how to do it by searching for a specific string and saving all that have that string, but since there are a lot of sites, is there a way to automate this?

Would using unique to make a list of the filenames and then looping through to create a file using that list work? I'm still relatively new to programming, so I don't know what I can do.

Was it helpful?

Solution

    filename = ('PM2.5_NY_2012.csv'); % PM2.5 88101 data from NY in the year 2012

% Use functions created by read_mixed_csv.m to read in
data = read_mixed_csv(filename,','); % Creates cell array of data
data = regexprep(data, '^"|"$',''); % Gets rid of double quotes at the start and end of the string 

% Sort data based on date (Column 1)
[Y,I] = sort(data(:,1)); % Create 1st column sorted
site_sorted = data(I,:); % Sort the entire array

u_id=unique(site_sorted(:,2)); % get unique id

for i=1:length(u_id)
    idx=ismember(site_sorted(:,2),u_id{i}); % extract index where the second column matches the current id value
    site_data = site_sorted(idx,:);
    save([u_id{i} '.mat'],'site_data');
end

this should do it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top