Hi all,
I am looking for some help on how one converts cells to string arrays without all the trailing space. I am sure I am making things too complex. What I am doing in short, is setting up a function that returns spectral indices based on literature-published indices. The basic information for each index will be stored in a structure, which eventually I might make a separate file.
If a user wants all the indices, rather than asking them to provide all the indices by name, I will allow a key word 'all'. At that point, I take the fieldnames of the structure to give me the fieldnames to use. This is where things go wrong, as the fieldnames are in cells. I have tried
indice_names = fieldnames(indices_specs);indice_names = char(indice_names);
But then all the strings have trailing spaces. I can probably trim those again, but.. Is there a more straightforward way to go about this?
To give you some insight, this is what I am working on debugging (And no, none of the function calls to JF_calculate_vegetation_index work yet. The function itself does not even have content yet):
function out_indices = jf_calculate_vegetation_indices(spectra, offset, indice_names) % Matlab: 2015b
% Purpose
% Calculate well-known hyperspectral indices vegatation indices
%
% Parameters
% "Spectra" Input spectra in in an array, colums individual spectra, rows: samples
% "offset" The first wavelength of the spectrum. 1nm spacings are expected
% "indices" The name of the indices to be calculated in an array.
%Use a string "all" for all indices to be returned.
%%get all set up
num_indices = max(size(indice_names)); [num_samples,num_bands] = size(spectra); wavelengths = [offset:offset+num_bands]; %%create an structure that specifies all the info needed for each index in the setup
indices_specs.GI = struct('type', 'ri', 'bands', [554,677], ... 'name', 'GI', 'fullname', 'Green index', 'author', 'smith', 'year', '1995'); indices_specs.ZTM = struct('type', 'ri', 'bands', [750, 710], ... 'name', 'ZTM', 'fullname', 'Zarco-Tejada & Miller', 'author', 'Zarco-Tejeda', 'year', '2001'); indices_specs.PSSRa = struct('type', 'ri', 'bands', [800,806], ... 'name', 'PSSRa', 'fullname', 'Pigment specific simple ratio a', 'author', 'blackburn', 'year', '1998'); indices_specs.PSSRb = struct('type', 'ri', 'bands', [800,635], ... 'name', 'PSSRb', 'fullname', 'Pigment specific simple ratio b', 'author', 'blackburn', 'year', '1998'); % Check whether the key word "all" was set for the indices. Then create a
% list of name labels from the structure
if(indice_names == 'all') indice_names = fieldnames(indices_specs)' num_indices = max(size(indice_names)); end %%run the indices_specs
for i=1:num_indices if(indices_specs.(indice_names(i)).type == 'other') out_indices.(indice_names(i)).name = indice_names(i); out_indices.(indice_names(i)).data = JF_calculate_vegetation_index(indices_specs.(indice_names(i)).type, spectra, indices_specs.(indice_names(i)).name); else out_indices.(indice_names(i)).name = indice_names(i); out_indices.(indice_names(i)).data = JF_calculate_vegetation_index(indices_specs.(indice_names(i)).type, spectra(:,indices_specs.(indice_names(i)).bands)); end end end
Best Answer