Structure indexing: Convert cell to string without trailing spaces

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 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

Più risposte (1)

Jelle
Jelle il 12 Feb 2018
Cool, thanks. Not exactly the code I was looking for but it helped me in the direction.
It now looks like this:
if(indice_names == 'all') % change to strcmp
indice_names = fieldnames(indices_specs);
num_indices = max(size(indice_names));
end
%%run the indices_specs
for i=1:num_indices
thisindex = indice_names{i};
% adjust the wavelengths for the offset in the inspectra
usedbands = indices_specs.(thisindex).wavelengths - (offset-1);
out_indices.(thisindex).name = thisindex;
out_indices.(thisindex).data = JF_calculate_vegetation_index(indices_specs.(thisindex).type, indices_specs.(thisindex).name, spectra(:,usedbands));
end
end
  2 Commenti
Walter Roberson
Walter Roberson il 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.
Jelle
Jelle il 12 Feb 2018
Hi Walter,
Thank you for your continued help!
I noticed the comparison operator too as I was posting the answer. As I was on my way home I just added the comment in there that I will have to make that strcmp.
Good point with the indice_names{i}. I did not realize that. (I am just learning the language, obviously..). Will adjust the code to what you suggested and not try to be too clever for my own good. Sorry!
I will do a bit of testing on my side with the code I have now. I am sure this will make it clear what exactly goes wrong with my version, helping me better understand the differences. A bit hard to get my head around all the different types of dataformats in matlab. And the manual is really only of good help for me once I know matlab and the data types better. So your help is highly appreciated!

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by