MATLAB Answers

Matlab: Select a variable name and find the corresponding value

조회 수: 25(최근 30일)
Can somebody explain me how i get some specific values after the = sign? The input File is a .subvar file format. I dont know how to jump in the right row and column to get the value. Do you have a matlab tutorial link for such a problem.
I need for example two specific values (after the = sign): The value of $_Wk1_lr_m and $_Wk1_voll_m
Result: 15601 and 33690
! Test
subvargroup.begin ($G_Wk1)
subvar( $_Wk1_lr_C_x, str = ' 0.019 ' )
subvar( $_Wk1_lr_m, str = ' 15601 ' ) ! [kg] lr
subvar( $_Wk1_lr_C_y, str = '-0.007 ' )
subvar( $_Wk1_lr_C_z, str = ' 1.644 ' )
subvar( $_Wk1_voll_m, str = ' 33690 ' ) ! [kg] voll
subvargroup.end ($G_Wk1)

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Cedric Wannaz
Cedric Wannaz 16 Apr 2020
편집: Cedric Wannaz 16 Apr 2020
Here is one way:
content = fileread( 'myTextFile.txt' ) ;
match = regexp( content, '(?<=\$_Wk1_lr_m,\s+str\s*=\s*''\s*)[^\s'']+', 'match' ) ;
Wk1_lr_m = str2double( match ) ;
match = regexp( content, '(?<=\$_Wk1_voll_m,\s+str\s*=\s*''\s*)[^\s'']+', 'match' ) ;
Wk1_voll_m = str2double( match ) ;
Note that:
  • If there are multiple subgroups in your file, you will get vectors with all the values for these variables.
  • There are many possible approaches that we can adapt to your specific use-case if you provide more information.
We can imagine all sorts of patterns. This one does the following:
  • Match one or more characters that are neither a white-space nor a single quote: [^\s']+ (the ' must be doubled not to interfer with MATLAB char array delimiters)
  • Preceeded by (?<=..) (this is a look-backward) the literal \$_Wk1_voll_m, ($ must be escaped) followed by one or more white-space \s+, followed by the literal str, followed by one or more white-space, followed by the literal ' followed by zero or more white-spaces \s*.

  댓글 수: 4

표시 이전 댓글 수: 1
Cedric Wannaz
Cedric Wannaz 20 Apr 2020
Hi Billy,
This is a good problem for training regular expressions! REGEXP can operate in many ways and output many different things. If you compare my solution to Stephen's solution, you see that I am asking REGEXP to output matches whereas Stephen is asking for tokens. Tokens are parts of matches. My pattern matches [^\s']+ which is the value, preceeded by what I define in the look-backward/behind. Stephen's pattern matches the whole 'Wk1_(lr|voll)_m\D+(\d+)' but then he asks for REGEXP to extract/output only parts of the match, i.e. the tokens, which are framed by parentheses in the pattern. There are two tokens, one is either the literal lr or the literal voll, and the other is one or more (+) element of the set defined by \d, which is the set of digits.
Extracting tokens is often more handy but it adds a level of cell indexing: when asked for matches, REGEXP outputs a cell array of matches (or just one matched string when asked to match only 'once'); when asked to output tokens, REGEXP outputs a cell array of cell arrays of tokens (or a cell array of tokens when asked to match only 'once'). Each cell of the outter cell array corresponds to a match, and for each match we get a cell array of tokens.
In the extension of your question, you want to extract two parts of a match: a variable name and a factor (or even three is the operator can vary?). This is where extracting tokens comes really handy.
In the following, we match once an expression that defines two tokens associated with variable Wk_leer_Ixx. The pattern macthes the literal _Wk_leer_Ixx, followed by as few characters as possible until we match the literal .$ (both are special chars and must be escaped) followed by one or more non-whitespace character \S+ that we define as a token (for further extraction, because they define the variable name that we seek) by framing it in parentheses, etc. until we define the second token [^']+ that captures one or more charcaters different from '.
content = fileread( 'myTextFile.txt' ) ;
match = regexp( content, '_Wk_leer_Ixx,.*\.\$(\S+)\s*\S\s*([^'']+)', 'tokens', 'once' ) ;
factor = str2double( match{2} ) ;
pattern = ['(?<=\$', match{1}, ',\s+str\s*=\s*''\s*)[^\s'']+'] ;
match = regexp( content, pattern, 'match' ) ;
Wk_leer_Ixx = str2double( match ) * factor ;
After the first call to REGEXP (where we ask to match only once and output tokens) is executed, variable match is a cell array of tokens. The first token is the variable name and the second is the factor (defined as a string). We can convert the latter to numeric/double, and then build the pattern for getting the value of the variable whose name is stored in match{1} with a second call to REGEXP.
Again, note that many other approaches are possible. We could for example extract all variables and all values, and build more complexe data structure that contain everything.
Hope it helps,
Billy Jones
Billy Jones 29 Apr 2020
Thank you so much for your long answer. I have translated your answer to my mother language. I understand most of your explanation. But for me as a beginner its hard to search for bugs in my code. If i use this input your code works not anymore.... Can you help me the last time .....
! Wagenkas
subvargroup.begin ($G_Wk)
subvar( $_Wk_leer_m, str = ' 15601 ' ) ! [kg] le
subvar( $_Wk_leer_CoG_x, str = ' 0.019 ' )
subvar( $_Wk_leer_CoG_y, str = '-0.007 ' )
subvar( $_Wk_leer_CoG_z, str = ' 1.644 ' )
subvar( $_Wk_leer_Ixx, str = ' $G_Wk.$_Wk_leer_m * 111 ' )
subvar( $_Wk_leer_Iyy, str = ' $G_Wk.$_Wk_leer_m * 222 ' )
subvar( $_Wk_leer_Izz, str = ' $G_Wk.$_Wk_leer_m * 333 ' )
subvar( $_Wk_betrieblich_m, str = ' 27660 ' ) ! [kg] betr
subvar( $_Wk_betrieblich_CoG_x, str = ' 0.0107' )
subvar( $_Wk_betrieblich_CoG_y, str = '-0.0039 ' )
subvar( $_Wk_betrieblich_CoG_z, str = ' 1.7608 ' )
subvar( $_Wk_betrieblich_Ixx, str = ' $G_Wk.$_Wk_betrieblich_m * 444 ' )
subvar( $_Wk_betrieblich_Iyy, str = ' $G_Wk.$_Wk_betrieblich_m * 555 ' )
subvar( $_Wk_betrieblich_Izz, str = ' $G_Wk.$_Wk_betrieblich_m * 666 ' )
subvar( $_Wk_voll_m, str = ' 33690 ' ) ! [kg] Wag
subvar( $_Wk_voll_CoG_x, str = ' 0.0088 ' )
subvar( $_Wk_voll_CoG_y, str = '-0.0032 ' )
subvar( $_Wk_voll_CoG_z, str = ' 1.788 ' )
subvar( $_Wk_voll_Ixx, str = ' $G_Wk.$_Wk_voll_m * 777 ' )
subvar( $_Wk_voll_Iyy, str = ' $G_Wk.$_Wk_voll_m * 888 ' )
subvar( $_Wk_voll_Izz, str = ' $G_Wk.$_Wk_voll_m * 999 ' )
subvargroup.end ($G_Wk)
Cedric Wannaz
Cedric Wannaz 29 Apr 2020
No problem, and actually I made a mistake that I did not see based on the small size of the input text.
I corrected the mistake (but you have to check) and updated a couple other things in the code below:
content = fileread( 'myTextFile.txt' ) ;
match = regexp( content, '_Wk_leer_Ixx,.*?\.\$(\S+)\s+\S\s*([^'']+)', 'tokens', 'once' ) ;
factor = str2double( match{2} ) ;
pattern = ['(?<=\$', match{1}, ',\s+str\s*=\s*''\s*)[^\s'']+'] ;
match = regexp( content, pattern, 'match' ) ;
Wk_leer_Ixx = str2double( match ) * factor ;
The issue was that I was using a greedy .* (in _Wk_leer_Ixx,.*\.) that would match as many characters as possible instead of the lazy .*? (now _Wk_leer_Ixx,.*?\.) that matches as few characters as possible. REGEXP was hence going as far as possible for matching what follows in the pattern, and getting '_Wk_voll_m * 999'.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Stephen Cobeldick
Stephen Cobeldick 16 Apr 2020
One simple regular expression:
str = fileread('test.subvar');
rgx = 'Wk1_(lr|voll)_m\D+(\d+)';
tkn = regexp(str,rgx,'tokens');
tkn = vertcat(tkn{:})
tkn =
'lr' '15601'
'voll' '33690'
Which of course you can easily convert to numeric:
>> vec = str2double(tkn(:,2))
vec =

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Community Treasure Hunt

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

Start Hunting!

Translated by