String and two variables

1 view (last 30 days)
Adrian Pielmeier
Adrian Pielmeier on 22 Jun 2018
Edited: Stephen23 on 22 Jun 2018
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 Comment
Stephen23
Stephen23 on 22 Jun 2018
Edited: Stephen23 on 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'

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 22 Jun 2018
Edited: Stephen23 on 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 Comments
Stephen23
Stephen23 on 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
Adrian Pielmeier
Adrian Pielmeier on 22 Jun 2018
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!

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!