assignment string values corresponding to index from cell array

Please have a look to the following code, how can I assign string values corresponding to the index (ind_logical) from cell (files) in to the new cell (filesout). Thanks
Files = dir(fullfile(pth, ncfiles));
files = arrayfun(@(X) fullfile(pth, X.name), Files, 'uni', 0);
%
% Format latlim,lonlim:
% Wrap longitudes to 180:
latlim = [41 48]; lonlim = [27 48];
lonlim(lonlim>180) = lonlim(lonlim>180)-360;
% If two elements are input, assume user wants floats bound by geoquad:
if numel(latlim)==2
latv = [latlim(1) latlim(1) latlim(2) latlim(2)];
lonv = [lonlim(1) lonlim(2) lonlim(2) lonlim(1)];
else
latv = latlim;
lonv = lonlim;
end
% Initialize arrays that we'll try to populate:
filesout = {};
latout = [];
lonout = [];
for lf = 1 : length(files)
%try
filename = files{lf,1};
lat = ncread(filename,'LATITUDE');
lon = ncread(filename,'LONGITUDE');
[inpoly,onpoly] = inpolygon(lon,lat,lonv,latv);
[inpoly,onpoly] = inpolygon(lon,lat,lonv,latv);
inpoly = inpoly+onpoly;
ind = inpoly; ind_logical = logical(ind);
latout = [latout;lat(ind_logical)];
lonout = [lonout;lon(ind_logical)];
%filesout = [filesout;files(ind_logical)];
%filesout = arrayfun(@(x) x , files(ind_logical), 'UniformOutput', false);
%end
end

4 Comments

It's not clear from your code what relationship there is between inpoly, a logical array based on the data in one file and the list of all files. Assuming that inpoly has equal or less elements than the list of files, you could use it to index that list as you have done, but are you sure that's really what you want to do?
Beside this, it's unclear why you have the same function call in two successive lines. It's going to return the same values each time. In addition, if you use a logical or | instead of + you don't need to bother converting back to logical and you certainly don't need 3 variables to do that
[inpoly ,onpoly] = inpolygon(lon,lat,lonv,latv); %don't need to do it twice
inpoly = inpoly | onpoly; %logical OR. inpoly stays logical, no need for ind, or ind_logical
latout = [latout; lat(inpoly)];
lonout = [lonout; lon(inpoly)];
However, note that as documented, inpoly also includes onpoly, so even the logical or is not needed:
inpoly = inpolygon(lon, lat, lonv, latv); %inpoly also includes points ON the polygon
latout = [latout; lat(inpoly)];
lonout = [lonout; lon(inpoly)];
Dear Guillaume,
Sorry for late replay, based on the specific lon and lat (latlim = [41 48]; lonlim = [27 48];) I would like to extract desired parameters from the list of nc files, appreciate any helpful comments. Thans
What are the desired parameters? If it's latout and lonout, you already have got these. If it's something else, please clarify.
From the 'nc' file listed in 'files' shown as form of cell array, I want to find The desired values, such as temperature, salinity, pressure from the each netcdf file based on the desired longitude and latitude, since the values in each '.nc file' are corresponding to the one longitude and latitude. The all netcdf files listed in files are global data (ARGO profile).

Sign in to comment.

Answers (0)

Asked:

on 29 Jan 2020

Commented:

on 30 Jan 2020

Community Treasure Hunt

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

Start Hunting!