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

2 ビュー (過去 30 日間)
Sam
Sam 2012 年 4 月 16 日
回答済み: Simon 2013 年 12 月 11 日
I am reading a text file into Matlab called 'test.txt' which is structured as follows:
$variable1 = answer1;
$variable2 = answer2;
$variable3 = answer3;
I read the text file into Matlab line by line using the following segment of code:
fid = fopen('test.txt.');
tline = fgetl(fid);
tracks = {};
while ischar(tline)
tracks{end+1} = regexp(tline, '(?<=^.*\=\s*)(.*)(?=\s*;$)', 'match', 'once');
tline = fgetl(fid);
end
fclose(fid);
This piece of code returns the value of each variable line by line and would output:
answer1
answer2
answer3
What I want to do is modify my regexp expression so that I can specify the name of the variable to retrieve and have Matlab output the value assigned to the variable specified.
E.g. If I specify in my code to find the value of $variable2, Matlab would return:
answer2
Regards
  1 件のコメント
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.

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

回答 (4 件)

Walter Roberson
Walter Roberson 2012 年 4 月 17 日
var2find = 'variable2';
tracks{end+1} = regexp(tline, ['(?<=^\$' var2find '\s*=\s*)([^ ;]*)(?=\s*;+\s*$)'], 'match', 'once');
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;;
However, the expression would have trouble with
$variable2 = 345; ;
If you want that to be acceptable you need to say whether the value should be '345;' or '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 = answer2;', ...
'$variable3 = answer3;'];
varToFind = '\$variable2\W+=\W+(.*?);';
found = regexp(text, [varToFind], 'tokens' );
found{1}

Andreas Justin
Andreas Justin 2013 年 12 月 11 日
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 = nope','e = 1e6'};
txt = regexprep(txt,'(.*)','$1\n');
txt = [txt{:}];
fprintf(fid,txt);
fclose(fid);
clear fid
%%getting 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
for ii = 1:numel(c)
keys{ii} = c(ii).getKey;
vals{ii} = c(ii).getValue;
end

Simon
Simon 2013 年 12 月 11 日
Hi!
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', 'delimiter', '\n');
fclose(fid);
FC = FC{1};
% find '=' to split lines
ind = (strfind(FC, '='));
% split in left and right hand part
lhp = strtrim(cellfun(@(x,y) x(1:y-1), FC, ind, 'un', 0));
rhp = strtrim(cellfun(@(x,y) x(y+1:end), FC, ind, 'un', 0));
% search for string in left hand part (handles even substrings)
tf = ~cellfun('isempty', strfind(lhp, 'able1'));
% show match
rhp(tf)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by