How to find the position of a specific characters containing in a string in a cell array

9 visualizaciones (últimos 30 días)
hello. i have a 136x39 cell array like this one
'Data Name' 'Timestamp' 360 370
'001-Day 1-RE-1' '24/07/2014 10:17:10' -738.803345332164 -734.184486898226
'001-Day 1-RW-1' '24/07/2014 10:17:37' -707.272295650121 -703.634423319119
'001-Day 1-LE-1' '24/07/2014 10:19:20' -744.635983922777 -740.346463517543
'001-Day 1-LW-1' '24/07/2014 10:19:39' -751.552763303339 -747.041101104821
and I want to find the positions of 'RE','RW','LE','LW'. I tried with the function [RW1, RW2]=find(strcmp('RW', array)); but got no results. I also tried [RW1, RW2]=find(strcmp('*RW*', Acdelta)); or strfind..
Any ideas?
Many thanks

Respuesta aceptada

Guillaume
Guillaume el 22 de Oct. de 2014
Use either strfind four times, once for each pattern (RE, RW, LE, LW). Note that strfind (and strcmp) does not accept wildcards, or regexp
pos = regexp(array(:, 1), 'RE|RW|LE|LW', 'once');
Note that you can't pass the full cell array to strfind or regexp, since it contains elements that are not strings. Here, I just pass the first column.
Also, in your example, the position is always the same, so why do you want to find it?
  4 comentarios
Jo
Jo el 22 de Oct. de 2014
yes you are right. 'RE', 'RW' is the 10th character in this array but it may change in a different one, that's why I am looking for 'RE', 'RW' only. Each of 'RE', 'RW', appears to have 4 different values for each day (day1, day2,..). So I want to find the positions (values) and calculate the mean for each day. In a different array, where 'LW' wasn't part of a string, I used the function [LW1, LW2]=find(strcmp('LW', array)), found the positions of LW and calculated the mean as I described above.
Guillaume
Guillaume el 22 de Oct. de 2014
Ok, I'm still not very clear on what you're trying to do and whether or not your question is actually answered.
If you want the position and the value that follows the 'RE/RW/LE/LW', you can use a different regular expression:
[pos, valuestr] = regexp(array(:, 1), '(?:RE|RW|LE|LW)-(\d+)', 'start', 'tokens', 'once');
values = cellfun(@(v) str2double(v), valuestr, 'UniformOutput', false);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by