Using if with char

4 次查看(过去 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