Compare two strings with some restrictions

1 view (last 30 days)
Hey, how are you?
I have to compare to strings of n and m lines each other to see if they have the same messages. The messages are the following way:
As you can see the last four numbers change from 0000 to 5959 the first two are minutes and the other two seconds. I have the code to compare all the messages from one script to another but now I have to compare just the messages that have and ending in a range that we put. Exemple:
This message ends with 0059 I should compare it with all the messages that end from the number 0000 and 0159. That makes a comparison with the numbers that are one minut above and up the message.
flashpode on 15 Sep 2021
Edited: flashpode on 18 Sep 2021
Okay one string is this one:
and the other string is:
so the output is another string that has the messages taht are in both strings

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Sep 2021
The result would have been a cell array of character vectors. You can str2double() to get a set of decimal numbers.
Once you have the set of decimal numbers, referred to below as DN, then
dur = minutes(floor(DN/100)) + seconds(mod(DN,100));
If you do that for both sets of data, getting dur1 and dur2, then
[~, M1, S1] = hms(dur1);
[~, M2, S2] = hms(dur2);
[has_match0, idx0] = ismember(M1, M2);
[has_match1, idx1] = ismember(M1+1, M2);
M1_has_match = has_match0 | has_match1;
M1_match(has_match1) = idx1(has_match1);
M1_match(has_match0) = idx0(has_match0);
M1_matches = find(M1_has_match);
M2_matches = M1_match(M1_has_match);
If I got everything right, then M1_matches will be the index into the first set of durations in which there are matches, and M2_matches will be the corresponding indexes into the second set of durations that match the first set.
Any one entry in the first set of durations is only looked for once in the second set of durations, but because of the matching process, any given entry in the second set of durations could match more than one entry in the first set of durations. You did not ask for the closest match that occurs within a particular time interval: you asked for matches that occur if the second set has any entry that has the same minute as one in the first set, or is the next minute after one in the first set.
flashpode on 23 Sep 2021
And the other one is so much clear to me

Sign in to comment.

More Answers (1)

chrisw23 on 22 Sep 2021
strEx = "!AIVDM,1,1,,A,137JlD52h0P9tdRGbCQSm0kV0<1p,0*4C0053 !AIVDM,1,1,,B,13EsReP00009vQ`Gbj65gPiV00Sd,0*7B0053";
% check/modify the expression under
exp = "(?<prefix>!\w*),(?<ident1>\d),(?<ident2>\d),,(?<ident3>\w),(?<strLoad>[\w\d:?<>@`]*),(?<time>[*\d\w]*)";
tbl = struct2table(regexp(strEx,exp,'names'))
This is just an example how to parse text by a simple grouped regular expression. I use the website described to write and test expressions. The table allows easy access for further processing (ie. datetime conversion) as previously shown. Look at string based compare methods like 'contains' or 'matches' , i.e. tbl.strLoad.contains("137JlD52h0P9td") -> results in logical index to access matches
Hope it helps
chrisw23 on 23 Sep 2021
Ur right "This is just an example... " and no 'best code' competition

Sign in to comment.


Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!