Coger los ultimos 4 digitos de cada fila de un string y hacer un string nuevo

3 views (last 30 days)
Buenas,
la linea de cada string es de esta forma:
!AIVDM,2,2,3,B,88888888880,2*240000
!AIVDM,1,1,,B,13GPhM0P01P9rGNGast>4?wn2@S7,0*7D0000
!AIVDM,1,1,,A,13ErMfPP00P9rFpGasc>4?wn2802,0*070000
!AIVDM,1,1,,A,33iMjv5P00P9wKdGcEOv4?v02DU:,0*460000
!AIVDM,1,1,,B,13FMMd0P0009o1jGapD=5gwl06p0,0*780000
!AIVDM,1,1,,A,4028ioivDfFss09kDvGag6G0080D,0*790000
!AIVDM,1,1,,A,D028ioj
!AIVDM,1,1,,A,13MAj;P00
!AIVDM,1,1,,B,19NS@=@01qP9tp4GQkJ0bh`200SP,0*780001
!AIVDM,1,1,,B,137FrD0v2u0:=4pGS;s6u5On00SJ,0*000001
y se tendria que coger los ultimos 4 digitos y hacer un string nuevo.
Muchas gracias

Answers (1)

Vaibhav
Vaibhav on 17 Apr 2024
Edited: Vaibhav on 17 Apr 2024
Hi flashpode
I understand that you want to extract the last four digits from each line (string) and create a new string using these extracted digits. You can achieve this by using a for loop to extract the last four digits from each line. Here is a code snippet to help you get started:
% Given strings
strings = {
'!AIVDM,2,2,3,B,88888888880,2*240000',
'!AIVDM,1,1,,B,13GPhM0P01P9rGNGast>4?wn2@S7,0*7D0000',
'!AIVDM,1,1,,A,13ErMfPP00P9rFpGasc>4?wn2802,0*070000',
'!AIVDM,1,1,,A,33iMjv5P00P9wKdGcEOv4?v02DU:,0*460000',
'!AIVDM,1,1,,B,13FMMd0P0009o1jGapD=5gwl06p0,0*780000',
'!AIVDM,1,1,,A,4028ioivDfFss09kDvGag6G0080D,0*790000',
'!AIVDM,1,1,,A,D028ioj',
'!AIVDM,1,1,,A,13MAj;P00',
'!AIVDM,1,1,,B,19NS@=@01qP9tp4GQkJ0bh`200SP,0*780001',
'!AIVDM,1,1,,B,137FrD0v2u0:=4pGS;s6u5On00SJ,0*000001'
};
% Initialize an empty string to store the extracted digits
newString = '';
% Extract the last 4 digits from each line
for i = 1:length(strings)
line = strings{i};
last4Digits = line(end-3:end);
newString = [newString, last4Digits];
end
% Display the new string
disp(['New string with last 4 digits: ', newString]);
New string with last 4 digits: 0000000000000000000000008ioj;P0000010001
Each line is treated as an individual string. If dataset is a single string with lines separated by newlines, you can create an array of strings by splitting on newlines.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!