Question

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.

Was it helpful?

Solution

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

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