How can I extract the full character string using a FOR loop?

I have an array of water quality values, I set any value that is fit for consumption to zero and only those unsuitable for consumption are left as nonzero values in the array. I then create a second array which is the index of each of the unsuitable values:
ind_unsuit = find (WQI_Belayhari_unsuit);
I want to use this index array to extract the names of the locations of each sample. The names of the all of the sampling locations are stored in a character array:
SL_Belayhari = char(SL_Belayhari);
I am able to extract the first character of each name using the following code:
loc_unsuit = ones(size(ind_unsuit));
for j=1:length(ind_unsuit);
[loc_unsuit(j)] = SL_Belayhari(ind_unsuit(j));
end
My question is, how can I extract the full character string, so that I can access the complete name of the location of each sample, rather than simply the first character?
Thank you in advance.

 Réponse acceptée

Is SL_Belayhari a character vector (string), or is it a character array in which each row is a location (blank padded if need be)?
Have you considered the alternative of using a cell array of strings instead of a character array?
SL_Belayhari = {'Station 1', 'Station 73', 'Blue Pump House'};
for j=1:length(ind_unsuit);
loc_unsuit{j} = SL_Belayhari{ind_unsuit(j)};
end
which could then be shortened to
loc_unsuit = SL_Belayhari(ind_unsuit);
with no "for" loop

1 commentaire

Thanks for your response, previously I set SL_Belayhari as a character array. Now trying what you suggest, I set it is a character vector and do the following:
ind_unsuit = find (WQI_Belayhari_unsuit);
The index array is as follows:
ind_unsuit = [1073; 1103; 1388; 1389; 1623; 1625; 1678]
loc_unsuit = SL_Belayhari(ind_unsuit);
However the result of this is:
loc_unsuit = [1]
To be clear can I just confirm how I would go about making SL_Belayhari a cell array of strings? I am importing this from excel?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by