String and two variables
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2018-6-22
编辑:Stephen23
2018-6-22
"...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'
回答(1 个)
Stephen23
2018-6-22
编辑:Stephen23
2018-6-22
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 个评论
Stephen23
2018-6-22
"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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!