Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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);
This piece of code returns the value of each variable line by line and would output:
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:
  1 Commento
Oleg Komarov
Oleg Komarov il 16 Apr 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.

Accedi per commentare.

Risposte (4)

Walter Roberson
Walter Roberson il 17 Apr 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 il 17 Apr 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' );

Andreas Justin
Andreas Justin il 11 Dic 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.
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{:}];
clear fid
%%getting startet
path = fullfile('D:\test1.txt');
fr =;
javaProp = java.util.Properties();
%%get one specific
%%get all in arrays
c = javaProp.entrySet.toArray
for ii = 1:numel(c)
keys{ii} = c(ii).getKey;
vals{ii} = c(ii).getValue;

Simon il 11 Dic 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', 'delimiter', '\n');
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


Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by