Assignment has more non-singleton rhs dimensions than non-singleton subscripts

1 view (last 30 days)
Hello all,
I sort of new to matlab but I'm trying to extract some data wind data from a netcdf file which has the variables time, longitude, latitude, and then data which is the wind data. I'm getting an error for the rhs dimension than non-singleton subscripts in line 38. Basically the netcdf consist of hourly data of wind speed.
%% Change your directory to where you have saved all your data files
clear all
clc
tic
%% Defining the parameters needed for obtaining appropriate data
% Specify the starting time in days
from_time = 0;
% Specify the time ending in days
to_time = 2;
% Specify longitude of the desired location
lon_user = -121.6757935;
% Specify latitude of the desired location
lat_user = 37.7457601;
%% Obtaining the data for the specified grid by looping through data files
% Counts the number of time
ntime = to_time-from_time+1;
for i=1:ntime % For every year, starting from the first one
time(i) = (from_time-1)+i; % Defines years to use in next step
filename = sprintf('u10m_card10_hrly_1979-2008.nobads.nc');
ncid = netcdf.open(filename,'NOWRITE'); % Returns the id of the file
varid_lon = netcdf.inqVarID(ncid,'lon'); % Returns the id of the variable 'lon'
lon = netcdf.getVar(ncid,varid_lon,0,161); % Returns the data from variable 'lon'
lon = lon-360; % Subtract 360 to get lon values in the correct range
varid_lat = netcdf.inqVarID(ncid,'lat'); % Returns the id of the variable 'lat'
lat = netcdf.getVar(ncid,varid_lat,0,200); % Returns the data from variable 'lat'
varid_time = netcdf.inqVarID(ncid,'time'); % Returns the id of the variable 'time'
time(:,i) = netcdf.getVar(ncid,varid_time,1,24) % Returns the data from variable 'time'
varid_data = netcdf.inqVarID(ncid,'data'); % Returns the id of the variable 'data'
windSpeed = ncread(filename,'data',[1,1,1],[161 200 24]); % Returns the data from variable 'data'
% The loop below identifies the closest grid to the location specified
% by the user
for m = 1:length(lon)
for j = 1:length(lat)
if abs(lon(m,1)-lon_user) < 0.05 && abs(lat(j,1)-lat_user)<0.05
grid_lon = m;
grid_lat = j;
end
end
end
windSpeed_all(:,i) = windSpeed(grid_lon,grid_lat,:); % Collects data for all of the time
end
% Reshapes 'tasmax' matrix into a column vector
windSpeed= reshape(windSpeed_all,[ntime*24,1]);
% Reshapes 'time' matrix into a column vector
time = reshape(time,[ntime*24,1]); %error is here
%% Write data into an XL file (aka 'windSpeeds.xlsx') that is created in the directory
% Specifies the format in which you want the output dates to be in
%formatOut = 'dd-mmm-yyyy';
% Returns the data numbers
%time_datenum = time + datenum('01-Jan-1900','dd-mmm-yyyy');
% Converts date numbers into dates
%date = datestr(time_datenum,formatOut);
% Creates a table with date and windSpeed data
T = table(windSpeed);
% Specifies the name of the excel file
%XL_filename = 'windSpeeds.xlsx';
% Writes the table in the excel file (make sure you don't have a file with
% the same name on your computer, an error might pop up)
%writetable(T,XL_filename);
toc
>> ncdisp('u10m_card10_hrly_1979-2008.nobads.nc')
Source:
E:\WindData\u10m_card10_hrly_1979-2008.nobads.nc
Format:
classic
Global Attributes:
history = 'Wed Oct 8 10:25:50 2014: ncks --mk_rec_dmn time old_bad_t_dim/u10m_card10_hrly_197901.nc u10m_card10_hrly_197901.nc'
NCO = '4.0.8'
Dimensions:
time = 262992 (UNLIMITED)
lat = 200
lon = 161
Variables:
time
Size: 262992x1
Dimensions: time
Datatype: single
Attributes:
missing_value = 1.000000015047466e+30
units = 'hours since 1900-01-01 00:00 UTC'
title = 'time'
long_name = 'time in hours since 1900-01-01 00:00 UTC'
lat
Size: 200x1
Dimensions: lat
Datatype: single
Attributes:
missing_value = 1.000000015047466e+30
units = 'degrees'
title = 'latitude'
long_name = 'latitude'
lon
Size: 161x1
Dimensions: lon
Datatype: single
Attributes:
missing_value = 1.000000015047466e+30
units = 'degrees'
title = 'longitude'
long_name = 'longitude'
data
Size: 161x200x262992
Dimensions: lon,lat,time
Datatype: single
Attributes:
missing_value = -999.99
units = 'm/s'
history = ' 2015/03/30 15:44:14 pierce: ncunary -out u10m_card10_hrly_1979-2008.nc

Answers (1)

Walter Roberson
Walter Roberson on 29 May 2019
You do not initialize time. Inside your "for every year" you assign to time(i) with a single subscript and with i=1 being first that is going to create time as 1x1. Then when you read in data you try to assign it to time(:, i). Since time is 1x1 then : on the first dimension is 1:1 and so that names a 1x1 area to output to. That is a problem unless you are reading in only a scalar.
Switching between single and double subscript confuses the reader.
  4 Comments
Richard Lau
Richard Lau on 29 May 2019
The intent of this code is to extract a specific wind data with using the provided longitude and latitude from the user and having the loop to identify the closeset coordinates that are within the variable of the netcdf files. The file goes from 1979-2008 with daily and hourly wind data.
Walter Roberson
Walter Roberson on 29 May 2019
At the end of for i=1:2 then what size() do you expect time to be, and which locations within it do you expect to have storing the values that you currently assigning with a single subscript?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!