how to read the parameter and parameter value from the text file
Show older comments
Hii everyone.. I want to know how to read the text file which contains a variable and its value and then can be used somewhere in the program of .m matlab file... for eg.. example.txt is the text file... whcich contains like this... 110.01 voltage 230 volts 110.02 Current 1.094 amps
now I want to read 110.01 & 110.02 as variable and 230 & 1.094 as their values respectively... but these values present in somewhere in the text... how to identify these parameters and their values... please anyone suggest the command which I have to use for the above said problem
regards Madhu
Accepted Answer
More Answers (2)
You're a bit vague about the structure of your text file. Is it all on one line? Are there any other numbers in your file? Does the text matter or just the number? What is the format of the number (are numbers like 1e4, NaN, -1246 possible?
Possibly, this will work for you:
numbers = str2double(regexp(fileread('example.txt'), '(?<=\s+|^)[+-]?\d*\.?\d+(?=\s+|$)', 'match'));
variables = numbers(1:2:end);
values = numbers(2:2:end);
Note: the regular expression will find any number enclosed by whitespace (or beginning or end of string) that conform to the following pattern: an optional + or - sign, followed by 0 or more digits, followed by an optional decimal separator, followed by 1 or more digits. That is the following are considered numbers:
-1.5
+5.6
-1
-.3
.6
1.78
8
The following are not
1.
1e6
NaN
Inf
4 Comments
per isakson
on 4 Mar 2015
Edited: per isakson
on 4 Mar 2015
How is the end of INVERTER DATA: indicated? By "112.26" in the first column?
To add to per's question:
- Is the number of entries fixed?
- Is the text in the middle fixed for a given code
- What kind of whitespace is responsible for the alignment of the value column? a variable number of blank spaces or a tab character?
- Is the format of the value fixed for a given code
- When the value is a number, should the unit be discared?
- When the value is a number, is it always integer?
- When it's a string what should be stored?
Could you show what you'd want as an output for your example?
The following will find the INVERTER DATA line and then parse each line until it does not match the following format: two number separated by a dot followed by an arbitrary number of alphabetic characters or spaces followed by at least two spaces followed by an arbitrary number of characters:
function parsed_data = decode_file(filename)
validateattributes(filename, {'char'}, {'row', 'nonempty'});
parsed_data = {};
fid = fopen(filename, 'rt');
tline = fgetl(fid);
while ischar(tline) && isempty(strfind(tline, 'INVERTER DATA'))
tline = fgetl(fid);
end
if ~ischar(tline)
error('end of file reach prematurely');
end
pattern = '^(\d+\.\d+)[A-Za-z ]+ {2,}(.*)$';
tline = fgetl(fid);
while ischar(tline)
parsed_line = regexp(tline, pattern, 'tokens', 'once');
if isempty(parsed_line) %not a code / value line
break; %stop processing
end
parsed_data = [parsed_data; parsed_line]; %#ok<AGROW>
tline = fgetl(fid);
end
fclose(fid);
end
Categories
Find more on Data Type Conversion 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!