Ik like to find a word (example:cat)in a structure field (by example: structureA.field1). How can you do that?

Antworten (1)

Birdman
Birdman am 30 Mär. 2018
Bearbeitet: Birdman am 30 Mär. 2018

0 Stimmen

One approach:
structureA=struct;
structureA.field1="cat";
strfind(structureA.field1,"cat")
It will return 1 since the number of cat word in second argument of strfind function is 1.

5 Kommentare

Stephen23
Stephen23 am 30 Mär. 2018
Bearbeitet: Stephen23 am 30 Mär. 2018
"It will return 1 since cat word occurs one time in your field."
Actually it will return 1 because that is the index position of where the string "cat" was found. strfind's output is not a count of how many times the string was found: the number of elements in the output corresponds to how many times the string was found.
Birdman
Birdman am 30 Mär. 2018
Ups yes, I read the documentation incorrectly. Let me edit it.
Stephen23
Stephen23 am 30 Mär. 2018
Erik Verdijk's "Answer" moved here:
structureA=struct;
structureA.field1="cat";
strfind(structureA.field1,"cat")
I get the message:
Error: File: negenentwintigmaart.m Line: 8 Column: 12
The input character is not valid in MATLAB statements or expressions.
What Did I wrong?
Stephen23
Stephen23 am 30 Mär. 2018
@Erik Verdijk: What version of MATLAB are you using?
Erik Verdijk
Erik Verdijk am 30 Mär. 2018
R2016a

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Tags

Gefragt:

am 30 Mär. 2018

Kommentiert:

am 30 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by