Trouble using split function in matlab

조회 수: 6 (최근 30일)
BA
BA 2022년 7월 22일
댓글: BA 2022년 7월 22일
Having some trouble w/ my data.
The dates are showing up wrong. For example:
3/9/9102
3/10/9102
Obviously, i didn't collect data in 9102 and if you read it backwards, its 2019 which would be the correct date.
Does anyone know how to correct for this?
For example, how would you convert
3/9/9102 --> 3/9/2019
If someone knows, let me know. Thanks
Someone helped me and gave me this code but unfortunately, it doesn't work for multiple dates in a column. It does work for one date though to reverse the year to 2019, it just doesn't work for multiple dates
%For just a single date, it works.
date = ['3/11/9102']
date = '3/11/9102'
newdate = split(date, '/');
newdate(3) = reverse(newdate(3));
newdate = char(join(newdate, '/'))
newdate = '3/11/2019'
%For multiple dates in a column, it doesn't work.
dates = ['3/11/9102'; '3/12/9102'; '3/13/9102'; '3/14/9102'; '3/15/9102';];
splitter = split(dates,'/');
Error using split
First argument must be text.
splitter(3) = reverse(splitter(3));
dates_new = char(join(splitter,'/'));
If you have a solution, please let me know! Thanks

채택된 답변

Akira Agata
Akira Agata 2022년 7월 22일
How about the following?
% Example
dates = {'3/11/9102'; '3/12/9102'; '3/13/9102'; '3/14/9102'; '3/15/9102'};
% Split by '/', apply fliplr function to '9102' and concatenate
c = split(dates, '/');
c(:, 3) = cellfun(@fliplr, c(:,3), 'UniformOutput', false);
datesNew = join(c, '/');
% Show the result
disp(datesNew)
{'3/11/2019'} {'3/12/2019'} {'3/13/2019'} {'3/14/2019'} {'3/15/2019'}
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 7월 22일
dates = {'3/11/9102'; '3/12/9102'; '3/13/9102'; '3/14/9102'; '3/15/9102'};
datesNew = regexprep(dates, '(\d)(\d)(\d)(\d)$', '$4$3$2$1')
datesNew = 5×1 cell array
{'3/11/2019'} {'3/12/2019'} {'3/13/2019'} {'3/14/2019'} {'3/15/2019'}
BA
BA 2022년 7월 22일
Both work great, thanks a lot guys!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by