|
|
if statement not recognized
name is a variable with 16 trials. the trials are listed as XXT1 and XXT2 but not necessarily after each other. I am using strcmpi to find the match when the first trial is current. But when I reach the second trial i am not going to find the first trial and 'match' becomes 17. But the if statement will not execute. Am i doing this wrong? Please help.
% g and e currently set to 1
for n = 1: length(name)
match = n+1;
while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
match = match +1;
end
if match==17
continue
end
disp(name(n))
disp(' matches with ')
disp(name(match))
end
|
|
0
|
|
|
|
Reply
|
Robert
|
9/15/2010 6:17:07 PM |
|
"Robert Longnecker" <rlongnec@hsc.unt.edu> wrote in message <i6r2j3$1ij$1@fred.mathworks.com>...
> name is a variable with 16 trials. the trials are listed as XXT1 and XXT2 but not necessarily after each other. I am using strcmpi to find the match when the first trial is current. But when I reach the second trial i am not going to find the first trial and 'match' becomes 17. But the if statement will not execute. Am i doing this wrong? Please help.
> % g and e currently set to 1
>
> for n = 1: length(name)
> match = n+1;
> while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
> match = match +1;
> end
>
> if match==17
> continue
> end
> disp(name(n))
> disp(' matches with ')
> disp(name(match))
> end
How do you know for a fact that the if statement is not executing?
Perhaps there is something wrong with your while loop or strcmpi logic.
Why not simply insert a "match" liine (with no semicoln)
before the if statement and see if match ever equals 17.
|
|
0
|
|
|
|
Reply
|
someone
|
9/15/2010 7:01:09 PM
|
|
"Robert Longnecker" <rlongnec@hsc.unt.edu> wrote in message <i6r2j3$1ij$1@fred.mathworks.com>...
> name is a variable with 16 trials. the trials are listed as XXT1 and XXT2 but not necessarily after each other. I am using strcmpi to find the match when the first trial is current. But when I reach the second trial i am not going to find the first trial and 'match' becomes 17. But the if statement will not execute. Am i doing this wrong? Please help.
> % g and e currently set to 1
>
> for n = 1: length(name)
> match = n+1;
> while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
> match = match +1;
> end
>
> if match==17
> continue
> end
> disp(name(n))
> disp(' matches with ')
> disp(name(match))
> end
Can you post an example of "name"?
Oleg
|
|
0
|
|
|
|
Reply
|
Oleg
|
9/15/2010 7:01:10 PM
|
|
"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i6r55m$n70$1@fred.mathworks.com>...
> "Robert Longnecker" <rlongnec@hsc.unt.edu> wrote in message <i6r2j3$1ij$1@fred.mathworks.com>...
> > name is a variable with 16 trials. the trials are listed as XXT1 and XXT2 but not necessarily after each other. I am using strcmpi to find the match when the first trial is current. But when I reach the second trial i am not going to find the first trial and 'match' becomes 17. But the if statement will not execute. Am i doing this wrong? Please help.
> > % g and e currently set to 1
> >
> > for n = 1: length(name)
> > match = n+1;
> > while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
> > match = match +1;
> > end
> >
> > if match==17
> > continue
> > end
> > disp(name(n))
> > disp(' matches with ')
> > disp(name(match))
> > end
>
> Can you post an example of "name"?
>
> Oleg
Thanks for the help guys. I know it reaches 17 because i see it my workspace, and i was outputting to the command line but did not have that include that in the earlier post.
Here is an example for name
'ALT1'
'ALT2'
'CST2'
'DVT1'
'DVT2'
'Emt2'
'Emt3'
'MGT1'
'MGT2'
'MNT2'
'MNT3'
'SNT2'
'SNT3'
'cst1'
'plt1'
'plt2'
I actually got the code to work by inserting a second if in the while loop. Don't know why it works but it does. This is the working code:
for n = 1: length(name)-1
match = n+1;
while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
match = match +1;
if match ==17
break
end
end
if match==17
disp('hey i exceed matrix dimensions')
continue
end
disp(name(n))
disp(' matches with ')
disp(name(match))
end
|
|
0
|
|
|
|
Reply
|
Robert
|
9/15/2010 7:25:24 PM
|
|
"Robert Longnecker" <rlongnec@hsc.unt.edu> wrote in message <i6r6j4$qjh$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i6r55m$n70$1@fred.mathworks.com>...
> > "Robert Longnecker" <rlongnec@hsc.unt.edu> wrote in message <i6r2j3$1ij$1@fred.mathworks.com>...
> > > name is a variable with 16 trials. the trials are listed as XXT1 and XXT2 but not necessarily after each other. I am using strcmpi to find the match when the first trial is current. But when I reach the second trial i am not going to find the first trial and 'match' becomes 17. But the if statement will not execute. Am i doing this wrong? Please help.
> > > % g and e currently set to 1
> > >
> > > for n = 1: length(name)
> > > match = n+1;
> > > while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
> > > match = match +1;
> > > end
> > >
> > > if match==17
> > > continue
> > > end
> > > disp(name(n))
> > > disp(' matches with ')
> > > disp(name(match))
> > > end
> >
> > Can you post an example of "name"?
> >
> > Oleg
>
> Thanks for the help guys. I know it reaches 17 because i see it my workspace, and i was outputting to the command line but did not have that include that in the earlier post.
>
> Here is an example for name
>
> 'ALT1'
> 'ALT2'
> 'CST2'
> 'DVT1'
> 'DVT2'
> 'Emt2'
> 'Emt3'
> 'MGT1'
> 'MGT2'
> 'MNT2'
> 'MNT3'
> 'SNT2'
> 'SNT3'
> 'cst1'
> 'plt1'
> 'plt2'
>
> I actually got the code to work by inserting a second if in the while loop. Don't know why it works but it does. This is the working code:
>
> for n = 1: length(name)-1
> match = n+1;
> while strcmpi(name{n}(1:end-1),name{match}(1:end-1))==0;
> match = match +1;
> if match ==17
> break
> end
> end
>
> if match==17
> disp('hey i exceed matrix dimensions')
> continue
> end
> disp(name(n))
> disp(' matches with ')
> disp(name(match))
> end
An alternative which finds the positions that match the names:
matches = cell(numel(name),1);
for c = 1:numel(name)
matches{c} = setdiff(find(~cellfun('isempty',strfind(name,name{c}(1:end-1)))),c);
end
[ 2]
[ 1]
[]
[ 5]
[ 4]
[ 7]
[ 6]
[ 9]
[ 8]
[11]
[10]
[13]
[12]
[]
[16]
[15]
Meaning that the first name matches with the second, the second with the first the third has no matches...
Oleg
|
|
0
|
|
|
|
Reply
|
Oleg
|
9/16/2010 7:58:04 AM
|
|
|
4 Replies
693 Views
(page loaded in 0.04 seconds)
|
|
|
|
|
|
|
|
|