line =
10 L1 L2 L5 C1 C2 P2 C5 S1 S2# / TYPES OF OBSERV
Name Size Bytes Class
line 1x81 162 char
%I need to use find function for each character in line. For example, I need to know which column is L1 or L2.

 Akzeptierte Antwort

Guillaume
Guillaume am 16 Apr. 2015
Bearbeitet: Guillaume am 16 Apr. 2015

0 Stimmen

Do you actually want to find which column of the string where L1 and L2 are, or if L1 is the second element and L2 the third?
If the former, then use Michael's answer. If the latter, then the best thing to do would be to split the string at the whitespaces then use strcmp:
line = '10 L1 L2 L5 C1 C2 P2 C5 S1 S2# / TYPES OF OBSERV';
splitline = strsplit(line);
L1idx = find(strcmp(splitline, 'L1'))
L2idx = find(strcmp(splitline, 'L2'))

4 Kommentare

sermet
sermet am 16 Apr. 2015
Bearbeitet: sermet am 16 Apr. 2015
I need to know which column, for example the answer for L1 must be 2 and for L2 it is 3 if we assume line as a 1*10 (one row, 10 columns) matrix.
When I perform strsplit(line) there are 2 unnecessary characters ('') was added to the line thus the result was wrong.
Guillaume
Guillaume am 16 Apr. 2015
Bearbeitet: Guillaume am 16 Apr. 2015
So, not the character position as in Michael answer
In that case, my answer is what you want.
sermet
sermet am 16 Apr. 2015
Bearbeitet: sermet am 16 Apr. 2015
In my case, your answer is related the topic but as I wrote in my comment the result was wrong.
Guillaume
Guillaume am 16 Apr. 2015
Bearbeitet: Guillaume am 16 Apr. 2015
I cannot reproduce your problem with the example given. My answer gives me 2 and 3 as output.
Can you find out the ASCII value of the extra characters, as it's not obvious what they are from your comment. You can get the ASCII value of a character by converting it to double, eg:
c = 'A';
asciival = double(c) %should return 65

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Michael Haderlein
Michael Haderlein am 16 Apr. 2015

1 Stimme

line='10 L1 L2 L5 C1 C2 P2 C5 S1 S2';
strfind(line,'L2')
ans =
13

Kategorien

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

Tags

Gefragt:

am 16 Apr. 2015

Bearbeitet:

am 16 Apr. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by