Reading parameters from a text file into the workspace
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.
Answers (1)
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.
Categories
Find more on Text Data Preparation 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!