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

2 vues (au cours des 30 derniers jours)
Sam
Sam le 16 Avr 2012
Réponse apportée : Simon le 11 Déc 2013
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 commentaire
Oleg Komarov
Oleg Komarov le 16 Avr 2012
Post few lines of your real text file and tell what you want to retrieve. Your regular expression doesn't make sense to me.

Connectez-vous pour commenter.

Réponses (4)

Walter Roberson
Walter Roberson le 17 Avr 2012
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 le 17 Avr 2012
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 le 11 Déc 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 = 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 le 11 Déc 2013
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)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by