Usage of strcmpi in embedded strings

suppose you have the following:
A = 'filename.exe'
B.B =
'whatisthatman.exe'
'nevermind.txt'
'ok.exe'
'\nevermind\filename.exe'
Is it possible to get the index of filename.exe that's embedded in that string with strcmpi?

3 Kommentare

That's not MATLAB code. Is B.B a cell array? Like this:
B.B = {...
'whatisthatman.exe'
'nevermind.txt'
'ok.exe'
'\nevermind\filename.exe'}
So the "B" field of B is an array of 4 cells, each cell with a different string in it?
Walter Roberson
Walter Roberson am 16 Jan. 2014
The outer B could be a structure array with 4 entries; then the output would be structure array expansion.
T
T am 16 Jan. 2014
Yes.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 16 Jan. 2014

0 Stimmen

No, strcmpi() always compares entire strings. "filename.exe" does not occur as the entire string in the 4th entry in B.B, so strcmpi() is not suitable. You will need to use regexp() or strfind()
Are you looking for the index within B(4).B, which would be 12, or are you looking for the index of the entry that contains the string, which would be the 4 ?
cellfun( @(S) ~isempty(strfind(A, S)), {B.B} )
would return [false false false true]

7 Kommentare

T
T am 16 Jan. 2014
Basically I want to retrieve the 4th row of B.B using the string in A.
tf = cellfun( @(S) ~isempty(strfind(A, S)), {B.B} );
B(tf).B
Be careful for the case where there are multiple matches.
Another approach:
T = regexp( {B.B}, ['.*' A '.*'], 'match' );
vertcat(T{:})
The T here would be a cell array that would have [] entries for the lines that do not match. The second line should have the effect of tossing out the [] entries. Again though there would be a problem if there were multiple matching lines.
T
T am 16 Jan. 2014
Bearbeitet: T am 16 Jan. 2014
B is of class struct but the message I get is
Error using regexp
All cells for regexp must be strings.
There error occurs because I have a row that has
[NaN]
What can I do with this?
Walter Roberson
Walter Roberson am 16 Jan. 2014
You could change your code so that it does not generate NaN -- e.g., perhaps you could use '' (empty string) instead.
T
T am 16 Jan. 2014
Bearbeitet: T am 16 Jan. 2014
Is there a way to remove [NaN] from
B.B = {...
'whatisthatman.exe'
'nevermind.txt'
'ok.exe'
'\nevermind\filename.exe'
[NaN]}
I tried using find(strcmpi(B.B,B.B(1))==1)
which works but I have specified the element B.B(1) that I wanted based on the index but I can't do that, it has to be the name ,A = 'filename.exe'.
Bfixed = {B.B};
Bfixed(isnan(Bfixed)) = {''};
T = regexp( Bfixed, ['.*' A '.*'], 'match' );
vertcat(T{:})
Bfixed(isnan(Bfixed))
Undefined function 'isnan' for input arguments of type 'cell'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

T
T
am 15 Jan. 2014

Kommentiert:

T
T
am 16 Jan. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by