Using if with char

조회 수: 6 (최근 30일)
Craig
Craig 2014년 5월 6일
답변: Craig 2014년 5월 6일
I have a string being read on a line by line basis which is working fine. Then I check for the $PSIMTPP string and subsequently store this as recordsraw. The string comes in a comma delimited format and I need to check for the third colum. If the third column is either a B C or M I need to store the string. There is another parts of the code that stores a log(loggedsets) but this is working so.
tpp = strfind(sample, '$PSIMTPP')
if tpp == 1
log(loggedsets).recordsraw(index).tpp = textscan(sample, '%s %s %s %s %n %s %s %n %n %n %n', 'delimiter',',');
if strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'B')
log(loggedsets).recordsb(index).tpp = log(loggedsets).recordsraw(index).tpp
elseif strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'C')
log(loggedsets).recordsc(index).tpp = log(loggedsets).recordsraw(index).tpp
elseif strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'M')
log(loggedsets).recordsm(index).tpp = log(loggedsets).recordsraw(index).tpp
end
index = index + 1
end
When I run this code everything works fine and I create the vectors I need but with one small problem. So for example in the string I have I have one B followed by 200 M and 200 C. The program creats 400 recordsm and 400 recordsc which are half empty.
I know its something to do with the if ifelse loop populating empty fields but not sure how to ammend?
Any ideas?

답변 (1개)

Craig
Craig 2014년 5월 6일
Sorry just realized it was a result of incorrect indexing. I needed to create three seperate index for each possible input.
if tpp == 1
log(loggedsets).recordsraw(index).tpp = textscan(sample, '%s %s %s %s %n %s %s %n %n %n %n', 'delimiter',',');
if strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'B')
log(loggedsets).recordsb(indexb).tpp = log(loggedsets).recordsraw(index).tpp
indexb = indexb +1
elseif strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'C')
log(loggedsets).recordsc(indexc).tpp = log(loggedsets).recordsraw(index).tpp
indexc = indexc +1
elseif strcmp(log(loggedsets).recordsraw(index).tpp{:,3}, 'M')
log(loggedsets).recordsm(indexm).tpp = log(loggedsets).recordsraw(index).tpp
indexm = indexm +1
end
index = index + 1
end

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by