Extract changes on a string

3 ビュー (過去 30 日間)
Arthur Romeu
Arthur Romeu 2020 年 1 月 8 日
コメント済み: Star Strider 2020 年 1 月 10 日
Hello everyone,
I am trying to come up with a solution for the following:
I have two vars with the same size: "status" and "dates". "status" refers to different status on an equipment, and I need only the cells where a change in status occurs. Let me illustrate
'Online' %01
'Online' %02
'Online' %03
'Online' %04
'Online' %05
'Online' %06
'Offline' %07
'Offline' %08
'Offline' %09
'Offline' %10
'Offline' %11
'Offline' %12
I need to extract from a list like this only lines like 06 and 07, which is the last entry of one status and the first entry of the other. However, it's not just that. I also have the "dates" on datetime, and they all correspond with each cell on "status". So in order to preserve the date information of each cell, I need to, in this example, get the information on lines 06 and 07 of the "status" list and also the date on lines 06 and 07 of the "dates" list.
I have no idea of where to begin with this code :(
I hope I explained everything as clearly as possible. I'm sending the referred lists as an attachment for better understanding.
Thanks in advance,
Arthur.

採用された回答

Star Strider
Star Strider 2020 年 1 月 8 日
編集済み: Star Strider 2020 年 1 月 9 日
Try this:
D1 = load('status.mat');
D2 = load('dates.mat');
status = string(D1.fontes);
datestimes = D2.data_tracking_tratado;
StatG = findgroups(status); % Create Numeric Labels For States
GrpID = unique(StatG); % Unique Labels
Trns = nchoosek(GrpID, 2); % Combinations Of All Status Changes
Trns = [Trns; fliplr(Trns)]; % Combinations Of All Status Changes
for k = 1:size(Trns,1)
StChng{k,:} = strfind(StatG.', Trns(k,:)); % First Index Of Status Changes — Indices Into Both ‘status’ & ‘datestimes’|
StChngIx{k,:} = Trns(k,:); % Associated Numeric Vector (Information Only)
end
StChngNE = cellfun(@(x) ~isempty(x), StChng); % Index To Delete Empty Entries
StChng = StChng(StChngNE); % Delete Empty Entries
StChngIx = StChngIx(StChngNE); % Delete Empty Entries
for k = 1:size(StChng,1) % Loop To Create Output Cell Arrays
Idx = StChng{k,1};
Out{1,k} = status(Idx);
Out{2,k} = status(Idx+1);
Out{3,k} = datestimes(Idx);
Out{4,k} = datestimes(Idx+1);
end
Other options, such as combining these into a table are also possible.
EDIT — (8 Jan 2020 at 21:45)
Added table code:
Outt = Out.'; % Transpose To (Nx4)
Status = [];
DatesTimes = [];
for k = 1:size(Outt,1)
Status = [Status; Outt{k,[1 2]}];
DatesTimes = [DatesTimes; Outt{k,[3 4]}];
end
StatusChanges = table(Status(:,1), Status(:,2), DatesTimes(:,1), DatesTimes(:,2), 'VariableNames',{'Status 1','Status 2','Time 1','Time 2'})
Producing:
StatusChanges =
Status 1 Status 2 Time 1 Time 2
____________ ____________ ____________________ ____________________
"Offline" "Soft Start" 01-Jan-2020 16:18:00 01-Jan-2020 16:19:00
"Offline" "Soft Start" 02-Jan-2020 12:34:00 02-Jan-2020 12:35:00
"Offline" "Test" 03-Jan-2020 09:02:00 03-Jan-2020 09:03:00
"Soft Start" "Test" 03-Jan-2020 09:55:00 03-Jan-2020 09:56:00
"Online" "Offline" 01-Jan-2020 12:15:00 01-Jan-2020 12:16:00
"Online" "Offline" 02-Jan-2020 08:23:00 02-Jan-2020 08:24:00
"Online" "Offline" 03-Jan-2020 05:50:00 03-Jan-2020 05:51:00
"Soft Start" "Online" 01-Jan-2020 16:39:00 01-Jan-2020 16:40:00
"Soft Start" "Online" 02-Jan-2020 13:06:00 02-Jan-2020 13:07:00
"Soft Start" "Online" 03-Jan-2020 10:52:00 03-Jan-2020 10:53:00
"Test" "Soft Start" 03-Jan-2020 09:52:00 03-Jan-2020 09:53:00
"Test" "Soft Start" 03-Jan-2020 10:31:00 03-Jan-2020 10:32:00
EDIT — (9 Jan 2020 at 02:10)
Corrected typographical errors.
  2 件のコメント
Arthur Romeu
Arthur Romeu 2020 年 1 月 10 日
Thanks a lot!!!
Seriousely man, you are very smart. Thanks for saving me. For how long have you been programming?
Star Strider
Star Strider 2020 年 1 月 10 日
As always, my pleasure!
I very much appreciate your compliment!
I’ve been programming on and off for a while. I took my first course in FORTRAN as an undergraduate in 1968, and was introduced to MATLAB in 1994. (I also had a career as a physician and biomedical engineer, now retired from both.)
I noticed that the rows are not chronological. To sort them by ‘Time 1’, add this line after the ‘StatusChanges’ table assignment:
StatusChanges = sortrows(StatusChanges,{'Time 1'});
Producing:
StatusChanges =
Status 1 Status 2 Time 1 Time 2
____________ ____________ ____________________ ____________________
"Online" "Offline" 01-Jan-2020 12:15:00 01-Jan-2020 12:16:00
"Offline" "Soft Start" 01-Jan-2020 16:18:00 01-Jan-2020 16:19:00
"Soft Start" "Online" 01-Jan-2020 16:39:00 01-Jan-2020 16:40:00
"Online" "Offline" 02-Jan-2020 08:23:00 02-Jan-2020 08:24:00
"Offline" "Soft Start" 02-Jan-2020 12:34:00 02-Jan-2020 12:35:00
"Soft Start" "Online" 02-Jan-2020 13:06:00 02-Jan-2020 13:07:00
"Online" "Offline" 03-Jan-2020 05:50:00 03-Jan-2020 05:51:00
"Offline" "Test" 03-Jan-2020 09:02:00 03-Jan-2020 09:03:00
"Test" "Soft Start" 03-Jan-2020 09:52:00 03-Jan-2020 09:53:00
"Soft Start" "Test" 03-Jan-2020 09:55:00 03-Jan-2020 09:56:00
"Test" "Soft Start" 03-Jan-2020 10:31:00 03-Jan-2020 10:32:00
"Soft Start" "Online" 03-Jan-2020 10:52:00 03-Jan-2020 10:53:00
The ‘StatusChanges’ table is now sorted chronologically!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by