String and two variables
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello Community,
In an excel File i got the following lines:
T_DCT_PK_WDT_W01B1_RANGE
T_DCT_PK_WDT_W02B1_RANGE
T_DCT_PK_WDT_W03B1_RANGE
T_DCT_PK_WDT_W04B1_RANGE
T_DCT_PK_WDT_W05B1_RANGE
the W..B. goes on and isnt in an right order.
So i would to do something like this but there is an error in my syntax:
str = ( 'T_DCT_PK_WDT_W%B%_RANGE' , i ; a )
I would like to have two variables in the string.
I hope you all understand
Thank you!
1 Kommentar
Stephen23
am 22 Jun. 2018
Bearbeitet: Stephen23
am 22 Jun. 2018
"...the W..B. goes on and isnt in an right order."
You could easily sort those char vectors into alpha-numeric order by downloading my FEX submission natsort, which sorts a cell array of char vectors by the characters and the values of any numeric substrings:
>> C = {...
'T_DCT_PK_WDT_W01B1_RANGE'
'T_DCT_PK_WDT_W02B1_RANGE'
'T_DCT_PK_WDT_W03B1_RANGE'
'T_DCT_PK_WDT_W04B1_RANGE'
'T_DCT_PK_WDT_W05B1_RANGE'};
>> C = C(randperm(numel(C))) % random order
C =
'T_DCT_PK_WDT_W01B1_RANGE'
'T_DCT_PK_WDT_W02B1_RANGE'
'T_DCT_PK_WDT_W03B1_RANGE'
'T_DCT_PK_WDT_W04B1_RANGE'
'T_DCT_PK_WDT_W05B1_RANGE'
>> D = natsort(C) % sort into alpha-numeric order
D =
'T_DCT_PK_WDT_W01B1_RANGE'
'T_DCT_PK_WDT_W02B1_RANGE'
'T_DCT_PK_WDT_W03B1_RANGE'
'T_DCT_PK_WDT_W04B1_RANGE'
'T_DCT_PK_WDT_W05B1_RANGE'
Antworten (1)
Stephen23
am 22 Jun. 2018
Bearbeitet: Stephen23
am 22 Jun. 2018
sprintf('T_DCT_PK_WDT_W%02dB%d_RANGE',i,a)
For example:
>> sprintf('T_DCT_PK_WDT_W%02dB%d_RANGE',5,1)
ans = T_DCT_PK_WDT_W05B1_RANGE
3 Kommentare
Stephen23
am 22 Jun. 2018
"And how do i now search for an specific number?"
Your question does not mention that you want to "search" for a specific number. What does this mean? What is the "number" that you want to search for, and where do you want to search for it?
This line of code hints that you apparently want to match a particular string:
if(i=5 %%a =2)
If this is the case you could do this by using regexp and str2double to extract the numbers:
>> str = 'T_DCT_PK_WDT_W05B1_RANGE';
>> str2double(regexp(str,'\d+','match'))
ans =
5 1
Siehe auch
Kategorien
Mehr zu Numeric Types 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!