String and two variables

2 次查看(过去 30 天)
Adrian Pielmeier
Adrian Pielmeier 2018-6-22
编辑: Stephen23 2018-6-22
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
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
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
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
Adrian Pielmeier
Adrian Pielmeier 2018-6-22
Sry for my bad english.
So i got an Excel file whith lines i posted at first. i want to write many if cases for each line in the excel so for example for the line with
T_DCT_PK_WDT_W01B1_RANGE
i want to give back a specefic funktion
i would like to use the variables i and a like you said in my if clause to ask if a=5 and i = 2 the do function
I hope u understand what i mean
Thx!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by