How to extract variables from Character array

5 ビュー (過去 30 日間)
gvreddy
gvreddy 2015 年 6 月 19 日
コメント済み: Stephen23 2015 年 7 月 8 日
Is it possible to get variable names from character array of only particular range?
For example, My char array and it looks like below
en:
variable_en1 = expression; variable_en2 += expression;
variable_en3 := expression;
variable_en4++;
variable_en5--;
du:
variable_du1= expression;
variable_du2 := expression
ex:
variable_ex1=0;variable_ex2=1;
variable_ex3 = 2;
I would like to extract only variable_en1 to variable_en5 in one array and variable_ex1 to variable_ex3 in another arry.
I am attaching character array .mat file.
Could you please help me?
  4 件のコメント
Stephen23
Stephen23 2015 年 6 月 19 日
編集済み: Stephen23 2015 年 6 月 19 日
What are "variables"? The question is not very clear.
You uploaded a .mat file containing one string. There are easy ways to extract parts of a string (particularly indexing or regular expressions), but you have not explained what part of the strings you are interested in. Please give exact examples of the desired output, and an explanation of how these should be identified (e.g. preceding or trailing characters, newline locations, character patterns, etc).
gvreddy
gvreddy 2015 年 6 月 19 日
編集済み: gvreddy 2015 年 6 月 19 日
That one string is from state flow state. I want to find the variabels in from en:. when you load .mat file, it gives one string. There I need to find left side varibles.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 6 月 19 日
編集済み: Stephen23 2015 年 6 月 19 日
Thank you for editing your question and making it clearer.
You can use regexp to locate these substrings. Here are several different versions using regexpi, which I tested on your sample .mat file:
>> regexpi(transDestiLabel,'^[a-z]+(?=\s\S?=)','match','lineanchors')
ans =
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'
>> regexpi(transDestiLabel,'^[a-z]+(?=+|-)','match','lineanchors')
ans =
'i' 'j'
>> regexpi(transDestiLabel,'^[a-z]+(?=\s\S?=|+|-)','match','lineanchors')
ans =
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'
The sample file:
  12 件のコメント
gvreddy
gvreddy 2015 年 7 月 8 日
編集済み: Guillaume 2015 年 7 月 8 日
Yes. This is what I expected..Thank you..
and other side, I am trying to understand regexp which you used to filter the text but I could not get well..
[C,S] = regexp(label,'(?<=\s|;|\:)\w+(?=(\s\S?)?(+|-|\:)?=)','match','start')
what I understood : (?<=\s|;|\:)\w matches string that follow : or ; and identifies word and
(?=(\s\S?)?(+|-|\:)?=) will matches white spaces and non-white spaces then followed by + or - or :
Is my understanding is correct or am I missing something?
I try to filter to get variables on one more char array which is attached to post but I am missing some variables. could you please check and let me know what is wrong?
Stephen23
Stephen23 2015 年 7 月 8 日
If you want to play around with regular expressions, try using my FEX submission, which lets you interactively build regular expressions and check them on a piece of text:
and keep reading this and trying examples until it all makes sense:
Lets break down the regular expression:
(?<=\s|;|\:)\w+(?=(\s\S?)?(+|-|\:)?=)
(?<=\s|;|\:) % preceded by whitespace, ; or :
\w+ % any alphanumeric word
(?= % followed by...
(\s\S?)? % maybe whitespace + non-whitepsace
(+|-|\:)? % maybe +, - or :
=) % equals sign
Hmmm... it seems like the \S? is not really required.
As I noted in an earlier comment the reasons this regular expression is so complicated is because the file format is a complete mess. If you can tidy up the file format, then identifying the variables becomes much easier.
Good luck!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by