Searching a string on a table to get time
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I have an excel spreadsheet (attached). The table is basically information from a ticket system. The column are as follows: ID, creation date & time, several comments (each one in a different column) and ticket closing date & time.
The first step I do is reading it: Tbl = readtable(filename, 'ReadVariableNames', false);
I want to calculate:
1) the time between when the ticket was acknowledged and the creation time
2) the time between when the ticket is asked to be closed and when it is actually closed.
A ticket is acknowledged in different ways, but it always says "your ticket".
A ticket is asked to be closed in different ways, it says: "can this be closed?", ''can we close this?", "is this still an issue?" or "are you happy to close this?"
So, what I'm thinking is: searching the table for key phrases (like "your ticket"), and then reading the time of the corresponding cell. However, how can I do this without using a for loop to go through the columns?
Thanks
0 个评论
回答(2 个)
Stephen23
2023-12-12
编辑:Stephen23
2023-12-12
"I still have the same issue about searching into all the Comment columns."
The MATLAB documentation explains that you can use PATTERN objects to specify the variables/columns:
tbl = readtable('test.xlsx');
pat = "Comment" + wildcardPattern;
tbl = convertvars(tbl,pat,'string')
idx = contains(tbl{:,pat},"your ticket")
vec = ["can this be closed?","can we close this?","is this still an issue?","are you happy to close this?"];
idy = contains(tbl{:,pat},vec)
Then you can use ANY, FIND, etc. as required to obtain the columns or rows that you require from the table. Note that not all rows have both start and end text.
3 个评论
Stephen23
2023-12-13
编辑:Stephen23
2023-12-13
"Unfortunately, it seems I can't use pattern with R2018a (it seems it started with 2020b)."
Obtain the column/variable names, use text tools to select the ones you want, then use the names you selected, e.g.:
tbl = readtable('test.xlsx');
pat = tbl.Properties.VariableNames; % changed this line
pat = pat(startsWith(pat,'Comment')); % changed this line
tbl = convertvars(tbl,pat,'string')
out = rowfun(@myfun, tbl, 'NumOutputs',2, 'OutputVariableNames',["acknowledge","askclose"])
out.acknowledge - tbl.Start
tbl.End - out.askclose
function [start,close] = myfun(varargin)
X = cellfun(@isstring,varargin);
S = [varargin{X}];
Y = find(contains(S,"your ticket",'IgnoreCase',true),1,'first');
Z = find(strlength(S)>0,1,'last');
Ty = split(S(Y),';');
Tz = split(S(Z),';');
start = datetime(Ty(1), 'Format','d/MMM/yy HH:mm:ss');
close = datetime(Tz(1), 'Format','d/MMM/yy HH:mm:ss');
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!