Structure indexing: Convert cell to string without trailing spaces
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Walter Roberson
on 12 Feb 2018
if strcmp(indice_names, 'all')
indice_names = fieldnames(indices_specs);
else
indice_names = cellstr(indice_names);
end
num_indices = length(indice_names);
%%run the indices_specs
for i=1:num_indices
thisname = incides_names{i};
if strcmp(indices_specs.(thisname).type, 'other')
out_indices.(thisname).name = thisname;
out_indices.(thisname).data = JF_calculate_vegetation_index(indices_specs.(thisname).type, spectra, indices_specs.(thisname).name);
else
out_indices.(thisname).name = thisname;
out_indices.(thisname).data = JF_calculate_vegetation_index(indices_specs.(thisname).type, spectra(:,indices_specs.(thisname).bands));
end
end
0 Comments
More Answers (1)
Jelle
on 12 Feb 2018
2 Comments
Walter Roberson
on 12 Feb 2018
indice_names == 'all' is wrong unless the user is passing in string objects (created with double quotes) instead of character vectors (created with single quotes.)
Using indice_names{i} is wrong if the user did not pass in 'all', unless they happened to pass in a cell array of character vectors or a string array. If they passed in a single character vector other than 'all' then it will not be a cell array. The adjustment for that is cellstr(), which will convert a character vector to a 1 x 1 cell array containing the character vector but will leave an existing cell array of character vectors alone.
See Also
Categories
Find more on String in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!