String Vector for-loop
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
zachary schneider
am 29 Aug. 2019
Kommentiert: Jos (10584)
am 30 Aug. 2019
I recently collected some point data, and the trial number or independent variable is a string, such as:
X = ['a1' 'a2' 'a3' 'b1' 'b2' 'b3'];
I would like to create a for loop using the [1 2 3 1 2 3]. Is there any way to seperate the number and letter, or a way to us a string such as this as a for loop condition.
I do not want to use the index, and am trying to make this work for a much larger set of data, so I am trying to have it work automatically.
3 Kommentare
Akzeptierte Antwort
Stephen23
am 29 Aug. 2019
Bearbeitet: Stephen23
am 29 Aug. 2019
>> str = {'a1','a2','a3','b1','b2','b3'};
>> vec = sscanf([str{:}],'%*[ab]%f',[1,Inf])
vec =
1 2 3 1 2 3
>> vec = str2double(regexp(str,'\d+$','match','once'))
vec =
1 2 3 1 2 3
>> vec = str2double(regexprep(str,'^[a-z]+',''))
vec =
1 2 3 1 2 3
3 Kommentare
Jos (10584)
am 30 Aug. 2019
This example strongly suggests that it is always a single digit following a single character that is relevant. If so, this will work too:
cha = ['a1 ';'a2 ';'a3 ';'b1 ';'b2 ';'b3 ']
vec = cha(:,2) - '0'
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!