Reading parameters from a text file into the workspace

7 Ansichten (letzte 30 Tage)
Pawan G
Pawan G am 18 Aug. 2017
Bearbeitet: Stephen23 am 18 Aug. 2017
Hi, I have a .txt file which has the following information:
'space'Total = 123
I would like to read some of this information into MATLAB. I tried using the following program to import the data but since there is a space before the line starts, i am not able to read the data.
fid = fopen(filename);
C = textscan(fid, '%s', 'Delimiter', '', 'CommentStyle', '%');
fclose(fid);
Total = C{2}(strcmp(C{1}, 'Total'));
could you please help me.

Antworten (1)

Guillaume
Guillaume am 18 Aug. 2017
Bearbeitet: Guillaume am 18 Aug. 2017
There are many ways you could do what you want. The way I'd do it:
filecontent = fileread(filename);
paramvalues = regexp(filecontent, '^\s*(\w+)\s*=\s*(\w+)\s*$', 'tokens', 'lineanchors');
paramvalues = vertcat(paramvalues{:});
Which should return a Nx2 cell array of parameter names (1st column) and values (2nd column), if I've not made a mistake in the regex.

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by