0

I got many strings from our device (array). There are two different kinds of strings :

  1. 011_c_srr_Rtzfi_at000_hh5_fs_v343_l067_i1_Test_Test
  2. 041_c_ddr_Rtzfi_ds000_hh5_fs_v343_l037_i1_Test_hall (the needed information is in the middle of the string)
  3. 061_t_err_Rsas_au000_ti3_fs_v777_l011_ *
  4. 021_t_err_Rsas_au230_ti3_fs_v777_l031_ (the needed information is at the end of the string)

Conclusion: I need the following part of the string l067 / l037 / l011 ..... For example l067 means 67% and I037 means 37%. So i need this two percent values. The result is 67 and 37. My Code: (only parts)

for j=1:numRows;
Name=LaSP.Messung(j, 1).name
Size=size(Name)
Length = strlength(Name)%Evaluiert die Länge des gesamten Strings
pos1 = findstr(Name, '_')%Listet alle "_" im String auf
[zeile1,spalte1]=size(pos1)
spalte=pos1(1,spalte1)%ich hol mir string position vom letzten "_"

if spalte==Length%Abfrage ob das letzte zeichen ist ein "_"
%%Abfrage ob der Wert schon einmal vorkam   
    %%die letzten 4 auslesen
else 
    %%in mitten des strings
end

      %pos = strfind(Name, '_')
 %k = strfind(Name,'_','ForceCellOutput',true)
 %idx = find(strcmp(Name, '_'))
 %pat='_';
%ind=regexp(Name,pat);
% word_to_find=strfind(strarray,'stringtofind');
% starray.index(word_to_find);
end

My problem is i can not split the string ... i can not extract the last 4 characters... Thank you

2
  • 1
    You may want to give a go to regular expressions Commented May 13, 2020 at 9:17
  • Thanks for posting your desired input & output and code, but what is the problem with the code? Were are you stuck? (Sidenote: whilst not disallowed on SO, having your in-code comments in German makes it more difficult to understand for non-German speakers). I'd do a strplit() on the dash, _, and then grab the ninth cell. Commented May 13, 2020 at 9:17

1 Answer 1

1

Given:

my_strings = {'011_c_srr_Rtzfi_at000_hh5_fs_v343_l067_i1_Test_Test',
'041_c_ddr_Rtzfi_ds000_hh5_fs_v343_l037_i1_Test_hall',
'061_t_err_Rsas_au000_ti3_fs_v777_l011_ *',
'021_t_err_Rsas_au230_ti3_fs_v777_l031_'};

few possible solutions here.

  1. Using regex to match, we can build our pattern to look for the characters after v followed by three digits and underscore (e.g. v777_)

    matches = regexp(my_strings,'v\d{3}_([^_]+)','tokens','once');
    [matches{:}]
    ans =
    
      1×4 cell array
    
      {'l067'}    {'l037'}    {'l011'}    {'l031'}
    
  2. Using regex to split on underscore, and then taking the 9th cell:

    split_strings = regexp(my_strings,'_','split');
    matches = cellfun(@(x) x(9),split_strings).'
    ans =
    
     1×4 cell array
    
     {'l067'}    {'l037'}    {'l011'}    {'l031'}
    
  3. Using strsplit to split on underscore, and then taking the 9th cell:

    n = numel(my_strings);
    matches = char.empty(0,n);
    for i = 1:n
      split_string = strsplit(my_strings{i},'_');
      matches = [matches split_string(9)];
    end
    matches =
    
      1×4 cell array
    
      {'l067'}    {'l037'}    {'l011'}    {'l031'}
    
Sign up to request clarification or add additional context in comments.

2 Comments

Hello one question again... What shall i do if it is not the 9th cell?
@FranzH Which cell is it? Just 9 to that number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.