Reading parameters from a text file into the workspace
7 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Guillaume
on 18 Aug 2017
Edited: Guillaume
on 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.
0 Comments
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!