Use regexp in Matlab to return the value of a variable from a text file

21次观看(最近30天)
山姆
山姆 2012年4月16日
Answered: Simon on 11 Dec 2013
我正在将文本文件读为名为“ test.txt”的MATLAB,该文件的结构如下:
$variable1 = answer1;
$变量2 =答案2;
$variable3 = answer3;
我使用以下代码段将文本文件通过行读为MATLAB线:
fid = fopen('test.txt。);
tline = fgetl(fid);
tracks = {};
尽管ischar(tline)
跟踪{end+1} = regexp(tline,'(?<=^.*\=\s*)(.*)(?=\s*;$)','匹配','一次');
tline = fgetl(fid);
end
fclose(fid);
This piece of code returns the value of each variable line by line and would output:
答案1
答案2
答案3
我要做的就是修改我的regexp表达式,以便我可以指定要检索的变量的名称,并使MATLAB输出分配给指定变量的值。
例如,如果我在我的代码指定to find the value of $variable2, Matlab would return:
答案2
Regards
1 Comment
Oleg Komarov
Oleg Komarov 2012年4月16日
Post few lines of your real text file and tell what you want to retrieve. Your regular expression doesn't make sense to me.

Sign in to comment.

答案(4)

Walter Roberson
Walter Roberson 2012年4月17日
var2find ='variable2';
跟踪{end+1} = regexp(tline,['(?<=^\$'var2find'\s*=\s*)([^ ;]*)(?=\s*;+\s*$)'],,'匹配','一次');
Note: this assumes that the values do not contain embedded blanks or semi-colons, but does not assume that the value is present. For example,
$variable2 = ;
would be treated as an empty response.
I have also tweaked the syntax accepted so that multiple semi-colons are accepted in case of typos:
$variable2 = 345;;
但是,表达可能会遇到麻烦
$variable2 = 345; ;
如果您希望可以接受,则需要说出该值是否应为“ 345”;或“ 345”。
I did not have difficulty reading the original regex, but I had to make guesses about what you wanted to have happen in case of extra semi-colons.

Ken Atwell
Ken Atwell 2012年4月17日
I cannot really follow your regular expression, but the following should help you get started:
text = ['$ variable1 = answer1;',...
'$ variable2 = anders2;',...
'$variable3 = answer3;'];
varToFind ='\$variable2\W+=\W+(.*?);';
found = regexp(text, [varToFind],'tokens');
found{1}

Andreas J.
Andreas J. on 11 Dec 2013
Since it's unanswered, and no answer is accepted. this one using java.util.Properties(), which propably is a nice way to get variable by names.
so this is for future search results.
%%preparing
fid = fopen(fullfile('D:\','test1.txt'),'w');
txt = {'#Variables','a = 1','b = false','c = true',' d =不','e = 1e6'};
txt = regexprep(txt,'(.*)','$ 1 \ n');
txt = [txt {:}];
fprintf(fid,txt);
fclose(fid);
清除支撑材
%%获得startet
path = fullfile('d:\ test1.txt');
fr = java.io.filereader(path);
javaProp = java.util.Properties();
javaprop.load(fr);
fr.close();
%%get one specific
javaProp.getProperty('a')
javaProp.getProperty('c')
javaProp.getProperty('d')
javaProp.getProperty('e')
%%get all in arrays
c = javaProp.entrySet.toArray
forii = 1:numel(c)
keys{ii} = c(ii).getKey;
vals {ii} = c(ii).getValue;
end

Simon
Simon on 11 Dec 2013
你好!
Maybe there is no need for a regexp. Read in the file, separate all lines at "=" and find the desired variable in the left hand part:
% read in file
fid = fopen('test.txt');
fc = textscan(fid,'%s',“定界符”,'\ n');
fclose(fid);
fc = fc {1};
% find '=' to split lines
ind =(strfind(fc,'=');
百分比在左和右手部分中分裂
lhp = strtrim(cellfun(@(x,y)x(1:y-1),fc,ind,,'联合国', 0));
rhp = strtrim(cellfun(@(x,y)x(y+1:end),fc,ind,'联合国', 0));
%在左手零件中搜索字符串(甚至是子字符串)
tf = ~cellfun('isempty', strfind(lhp,'able1');
%显示比赛
rhp(tf)

Tags

社区寻宝

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

开始狩猎!