Compare Strings in a loop

조회 수: 2 (최근 30일)
Kaan Aydin
Kaan Aydin 2020년 8월 15일
댓글: Kaan Aydin 2020년 8월 16일
Hello everybody,
I have the followong question.
In a struct I have two cell arrays CellA and CellB. These cell arrays are identically built and contain the information "DataA, DataB, Name"
What I want to do ist to compare the strings of "Name" of both Cell arrays. If they are the same I want to calculate CellA.DataA - CellB.DataA
The calculated values are saved in a third cell array CellC. This is my actual script.
i=length(data.CellB);
a=1;
for k = 1:length(data.CellA)
while a < i
tf = strcmp(data.CellA{k, 1}.Reifen, data.CellB{a, 1}.Reifen);
if tf == 1
data.CellC{k,1} = data.CellA{k, 1}.DataA - data.CellB{a, 1}.DataA;
data.CellC{k,2} = data.CellA{k, 1}.DataA - data.CellB{a, 1}.DataA;
data.CellC{k,3} = data.CellA{k, 1}.DataB - data.CellB{a, 1}.DataB;
data.CellC{k,4} = data.CellA{k, 1}.DataB - data.CellB{a, 1}.DataB;
a=a+1;
else
data.Pegeldelta{k, 1}=0;
data.Pegeldelta{k, 2}=0;
data.Pegeldelta{k, 3}=0;
data.Pegeldelta{k, 4}=0;
end
end
end
But it seems like I´m stuck in an endless loop.
I think the problem is at "a". After the a=a+1 iteration it doesn´t get set back "a" so the loop doesn´t start again at 1 for the next comparison.
Hope you can help me with this.

채택된 답변

Rik
Rik 2020년 8월 15일
If you know the number of interations, why not use a for loop again?
for k = 1:size(data.CellA,1) %use size(___,dim) instead of length() to get predictable behavior, use numel if you want all elements
A = data.CellA{k, 1}; %make the code more readable with a temp variable
for a = 1:size(data.CellB,1)
B = data.CellB{a, 1};
tf = strcmp(A.Reifen, B.Reifen);
if tf %don't compare a logical to 1
data.CellC{k,1} = A.DataA - B.DataA;
data.CellC{k,2} = A.DataA - B.DataA;
data.CellC{k,3} = A.DataB - B.DataB;
data.CellC{k,4} = A.DataB - B.DataB;
else
data.Pegeldelta{k, 1}=0;
data.Pegeldelta{k, 2}=0;
data.Pegeldelta{k, 3}=0;
data.Pegeldelta{k, 4}=0;
end
end
end
  댓글 수: 1
Kaan Aydin
Kaan Aydin 2020년 8월 16일
Not all heros wear capes. Thank you very much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by