I would like to read from a text file that has the format of the following:
The purpose is to use this as an input file to import other files, HELLO.txt and GOODBYE.txt (in the same directory - /Users/asdf/xyz), and assign values for a few variables (XVALUE = 1 and YVALUE = 2).
Any help will be appreciated. Thanks!
No products are associated with this question.
Easiest way is to parse with regular expressions and read into an output structure with named fields.
%read in file str = fileread('/path/to/your/file.txt'); %extract fields and values pieces = regexp(str, '(?<field>\w+)=(?<value>[^\n]+)', 'names'); out = struct(); %your variables for k=1:length(pieces), field = pieces(k).field; val = pieces(k).value; tf= ismember(val, char([34 39])); %remove " and ' from string val = strtrim(val(~tf)); %also remove spaces %try to convert to double. If successful, use converted value, %otherwise use original (string) value valn = str2double(val); if ~isnan(valn), %conversion successful, use as numeric val = valn; end %add to output structure out.(field) = val; end
Play games and win prizes!Learn more