Hi everyone,
Number of machines activated and hence the duration timestamp, varies each day so the dataset dimention is dynamic.
I am trying to extract the dataset for each machine. Cant get my extractBetwen () to work.
w=[ "Mac_A"
"1 9:53.3"
"2 9:23.5"
"3 2:16.2"
"4 2:45.6"
"5 12:01.2"
"Mac_B"
"1 23:56.5"
"2 28:12.6"
"3 15:34.1"
"4 19:23.0"
"5 1:38.4"
"Mac_C"
"1 11:35.6"]
% ..
% many more machines and thier datasets
l=length(w(strlength(w)==5)) % counts how many machines
for e=1: length(w)
start = w(strlength(w)==5)
stop = w(strlength(w)<8)
itsdata(data(l),:) = extractBetween(w,start,stop) % cant get this to work.
end
expected output is vectors which are the datasets of the machines.
["1 9:53.3,2 9:23.5,3 2:16.2,4 2:45.6,5 12:01.2"]
["1 23:56.5 , 2 28:12.6,3 15:34.1,4 19:23.0,5 1:38.4"]
["1 11:35.6"]
..
.. %more vectors

 채택된 답변

Jan
Jan 2021년 2월 9일
편집: Jan 2021년 2월 11일

1 개 추천

w=[ "Mac_A"
"1 9:53.3"
"2 9:23.5"
"3 2:16.2"
"4 2:45.6"
"5 12:01.2"
"Mac_B"
"1 23:56.5"
"2 28:12.6"
"3 15:34.1"
"4 19:23.0"
"51:38.4"
"Mac_C"
"1 11:35.6"];
Sep = [find(startsWith(w, 'Mac_')); numel(w) + 1];
n = numel(Sep) - 1;
itsdata = strings(n, 1);
for k = 1:n
itsdata(k) = [w{Sep(k)+1:Sep(k+1)-1}];
end
The command extractBetween() cuts out a piece of a string, not an array of strings between indices.

댓글 수: 7

klb
klb 2021년 2월 9일
편집: klb 2021년 2월 9일
Hi Jan, returns the last extraction only i.e. For Mac_C. it doesnt store the other datasets. If the loop can output an array (rather than vector as I ask) where each row is the string dataset of each machine, I could work with that too. expected output would look like:
itsdata =
"1 9:53.3,2 9:23.5,3 2:16.2,4 2:45.6,5 12:01.2"
"1 23:56.5 , 2 28:12.6,3 15:34.1,4 19:23.0,5 1:38.4"
"1 11:35.6"
...
Jan
Jan 2021년 2월 9일
I had a typo in my code. See above: "(k)" inserted to assign the k.th string of itsdata instead of overwriting it.
klb
klb 2021년 2월 11일
편집: klb 2021년 2월 11일
Inside the loop, getting "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." error.
Jan
Jan 2021년 2월 11일
Please try it again with the current code. I do not get the error message you have posted.
klb
klb 2021년 2월 13일
편집: klb 2021년 2월 13일
Expected output is with a comma sparator:
"1 9:53.3,2 9:23.5,3 2:16.2,4 2:45.6,5 12:01.2"
the code returns is
1 9:53.32 9:23.53 2:16.24 2:45.65 12:01.2
i.e the first value should be 1 9:53.3, but it has combined the serial no. 2 to it and results in 1 9:53.32.
Hence, modified the original dataset by adding a "," at the end of each row.
Works but crude. Could you suggest another way to obtain the Expected output as requested?
Can you also explain this command : Sep = [find(startsWith(w, 'Mac_')); numel(w) + 1]
w=[ "Mac_A"
"1 9:53.3"
"2 9:23.5"
"3 2:16.2"
"4 2:45.6"
"5 12:01.2"
"Mac_B"
"1 23:56.5"
"2 28:12.6"
"3 15:34.1"
"4 19:23.0"
"5 1:38.4"
"Mac_C"
"1 11:35.6"];
w= w+ "," % i added this.
Sep = [find(startsWith(w, 'Mac_')); numel(w) + 1]
n = numel(Sep) - 1;
itsdata = strings(n, 1);
for k = 1:n
itsdata(k) = [w{Sep(k)+1:Sep(k+1)-1} ];
end
itsdata
w=[ "Mac_A"; "1 9:53.3"; "2 9:23.5"; "3 2:16.2"; ...
"4 2:45.6"; "5 12:01.2"; "Mac_B"; "1 23:56.5"; ...
"2 28:12.6"; "3 15:34.1"; "4 19:23.0"; "5 1:38.4"; ...
"Mac_C"; "1 11:35.6"];
Sep = [find(startsWith(w, 'Mac_')); numel(w) + 1]
n = numel(Sep) - 1;
itsdata = strings(n, 1);
for k = 1:n
itsdata(k) = join(w(Sep(k)+1:Sep(k+1)-1), ',');
end
itsdata
klb
klb 2021년 2월 13일
편집: klb 2021년 2월 13일
Jan, Thank you so very much!
This expresion : Sep = [find(startsWith(w, 'Mac_')); numel(w) + 1], is complex so broke it into its elements and echoed each output. I understand that logic as well now. Thank you again for your time.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Audio Toolbox에 대해 자세히 알아보기

질문:

klb
2021년 2월 8일

편집:

klb
2021년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by