0

I have a code that creates a bunch of .mat files, but I want to save them as netcdf files (csv or txt would be fine as well) so people who can't use MATLAB can access them. This is what I have so far

%% Use function 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 
data = data(:,2:2:41); % Keep only the even cells because the odd ones are just commas

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

%% Find unique value in the site data (Column 2) 
% Format of site code is state-county-site
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');
    cdfwrite([u_id{i} '.nc'], 'site_data');
end

Everything works until the second to last line. I want to write each 'site_data' as a netcdf file with the same name as save([u_id{i} '.mat'],'site_data');, which is a string from the second column.

Daniel
  • 36,610
  • 3
  • 36
  • 69
SugaKookie
  • 780
  • 2
  • 17
  • 41

1 Answers1

1

Try

cdfwrite([u_id{i}],{'site_data',site_data})

The extension will be '.cdf'. I am not sure if this can be changed while using cdfwrite.

Edit: Corrected Typo

Mehrdad A.
  • 91
  • 3
  • You're missing an underscore on the second 'site_data'. MATLAB understood though. Thanks for the help. – SugaKookie Oct 05 '13 at 16:26
  • Actually, sorry, but though a netcdf file is created, it's mumble jumble. I'm thinking maybe because the variable is a cell? Do you know how to fix this issue? Also, how would I make the variables stored in the .mat file u_id{i} instead of just site_data for every one of them? – SugaKookie Oct 05 '13 at 17:04
  • I shall check and get back to you in a bit. – Mehrdad A. Oct 05 '13 at 18:05
  • I can open the cdf file I create with this code with cdfread with no problems. If you mean that you want to store more variables, then you just add the variables like so: `cdfwrite([u_id{i}],{'site_data',site_data,'nextVarName',nextVarValue,...,'LastV‌​arName',lastVarValue});` – Mehrdad A. Oct 05 '13 at 18:51
  • When I try cdfread, I get just one column with all the data going down. How can I keep the original format? The csv file I'm using can be found here: https://www.dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo – SugaKookie Oct 05 '13 at 20:25
  • Not sure if you can keep the same format in cdf file. Do you need that particular format? Why don't you output it as a text file? – Mehrdad A. Oct 05 '13 at 20:51
  • That works as well. How would I do a CSV format? Or txt I guess. – SugaKookie Oct 05 '13 at 20:54
  • Had a play with you mat file. Simplest but most inefficient solution is to write these as excel spreadsheets `xlswrite([u_id{i} '.xlsx'],site_data);`. You have string data which renders `csvwrite` useless. Lookup `fprinf` for writing into a text file. – Mehrdad A. Oct 05 '13 at 21:58