Effacer les filtres
Effacer les filtres

How do I find and replace strings in a cell array with numerical values?

2 vues (au cours des 30 derniers jours)
Brad
Brad le 4 Mar 2017
Commenté : Brad le 4 Mar 2017
I've got the following 3 x 5 cell array;
10000.1110000000 20000.9000000000 100.450000000000 22445 'SET_THRESH'
20000.2220000000 20000.9000000000 200.670000000000 22445 'HOLD_THRESH'
30000.3330000000 20004.6000000000 300.890000000000 22445 'DELETE_THRESH'
I'm trying to come up with a technique where the strings in column 5 are automatically replaced with their numerical equivalents (SET_THRESH is 0, HOLD_THRESH is 2, DELETE_THRESH is 3). The resulting 3 x 5 cell array would look like this;
10000.1110000000 20000.9000000000 100.450000000000 22445 '0'
20000.2220000000 20000.9000000000 200.670000000000 22445 '2'
30000.3330000000 20004.6000000000 300.890000000000 22445 '3'
Is it possible to achieve the desired output using a combination of functions within a loop?

Réponse acceptée

Walter Roberson
Walter Roberson le 4 Mar 2017
possible_threshes = {'SET_THRESH', 'HOLD_THRESH', 'DELETE_THRESH'};
thresh_vals = [0, 2, 3]
[tf, idx] = ismember(YourCell(:,5), possible_threshes);
YourCell(tf, 5) = num2cell( thresh_vals( idx(tf) ) );
Any entry that is unmatched will be left as a string.
  1 commentaire
Brad
Brad le 4 Mar 2017
Walter, I completely forgot about that option for ismember. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by