Create new field within struct based on other fields within same struct

조회 수: 3 (최근 30일)
Heinz Herrmann
Heinz Herrmann 2018년 11월 29일
댓글: Jan 2018년 12월 3일
Hi everybody,
I have a 1x24617 struct array with 12 fields and I am slowly descending into desperation.
Within a certain array of this structure there are 4 different types of videos listed which themselves are numbered from 1 to 10
i.e.
"vid8_010.avi"
What I want to do is to create a new field within the struct where every video that starts with "vid8" is converted to "fast", every video that starts with "vid4" is converted to "slow", etc.
'vid8_010.avi' 'fast'
'vid4_001.avi' 'slow'
'vid8_008.avi' 'fast'
My try so far has been extracting the field as a new array and try to find the certain video types with a contains function and then creating my new field dpending on the logicals given in "TF"
temp = struct2cell(METAKETA_behave.').'; % creates a new matrix containing the field vid_str (which is the fifth - hence (:, 5))
temp2 = temp(:, 5);
clear temp;
vid_str = string(temp2);
clear temp2;
pattern = "int8"; % indicate pattern which function should look for
TF = contains(temp2,pattern);
TF = TF';
%I would here follow up with an IF-Statement, but I have not yet gotten as far
My problem is that the last line of the array is listed as a double and I can't figure out how to convert it to a string so I can follow up on my method.
My final question is thus twofold:
1) is there an easier way to achieve what I am trying to do and
2) if not how can I solve my array-problem with the last entry not being the same data-type?
I know this is a lot, but I would really appreciate an answer as I just started to use MATLAB
Thank you all!
  댓글 수: 3
Jan
Jan 2018년 11월 30일
Omit the clear statements, because they are not useful. Most of all in:
temp2 = temp(:, 5);
vid_str = string(temp2);
clear temp2;
TF = contains(temp2,pattern);
In the last line temp2 does not exist anymore.
I do not understand the question. E.g. "how can I solve my array-problem with the last entry not being the same data-type" - all elements of an array have the same type except for cells. It is not clear which array you mean and which problem you want to solve.
every video that starts with "vid4" is converted to "slow" - Do you mean "every string" instead of "every video"? Why do you search for "int8", instead of "vid8"?
Heinz Herrmann
Heinz Herrmann 2018년 12월 2일
편집: Heinz Herrmann 2018년 12월 2일
"I do not understand the question. E.g. "how can I solve my array-problem with the last entry not being the same data-type" - all elements of an array have the same type except for cells. It is not clear which array you mean and which problem you want to solve."
I am sorry, what I meant was, that the field currently is being converted to a cell, not an array. I got the annotation wrong there. Can I convert a field to an array where all datapoints are of the same type? I have been looking in the forum, but I haven't been able to find a solution.
every video that starts with "vid4" is converted to "slow" - Do you mean "every string" instead of "every video"? Why do you search for "int8", instead of "vid8"?
Ok, this was my mistake, I changed the code a bit to make it clearer, "int8" is supposed to be named "vid8" here, but in my code it goes by int. Just thought it would clarify things more. I actually meant every string that starts with "vid8" should take the string "fast" in a new field next to the one with "vid8" in it

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

답변 (1개)

Jan
Jan 2018년 11월 30일
편집: Jan 2018년 11월 30일
With some bold guessing:
name = {METAKETA_behave.Field}; % Replace 'Field' by the name of your field
name(strncmp(name, 'vid8', 4)) = {'fast'};
name(strncmp(name, 'vid4', 4)) = {'slow'};
[METAKETA_behave.Field] = deal(name{:});
I cannot test this due to the lack of a small set of input data. Maybe a loop is easier:
for k = 1:numel(METAKETA_behave)
name = METAKETA_behave(k).Field; % Replace 'Field'
if strncmp(name, 'vid8', 4)
METAKETA_behave(k).Field = 'fast'; % Replace 'Field'
elseif strncmp(name, 'vid4', 4)
METAKETA_behave(k).Field = 'slow'; % Replace 'Field'
end
end
  댓글 수: 2
Heinz Herrmann
Heinz Herrmann 2018년 12월 2일
Thank you for your code, it works perfectly. However, would I be able to do that within the struct array itself, without having to create a cell?
So I could maybe copy the field, paste it next to the copied one and then execute your code within my struct array, so I have a new field in which "vid8" etc would be renamed, but with the original naming next to the new labels.
Jan
Jan 2018년 12월 3일
@Heinz: The 2nd code works inside the struct already without creating a cell. Maybe the conversion to a cell is more efficient then the loop, because internally structs and cells are represented almost equally and the conversion is very fast.
I do not understand the second paragraph. What does "copy the field, paste it next to the copied one" mean? You can use both methods to create new fields also.

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

Community Treasure Hunt

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

Start Hunting!

Translated by