how to read the parameter and parameter value from the text file
24 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Stephen23
2015-3-4
编辑:Stephen23
2015-3-4
The simplest solution is to read all of the data as a string, and split this into a cell array using regexp:
S = fileread('temp.txt');
C = regexpi(S,'^(\d+\.\d+) (.{20}) (.+?)$','tokens','lineanchors');
C = strtrim(vertcat(C{:}));
That's it! The value 20 is the width of the middle column. The variable C has three columns: the first has strings of the first values, the second has descriptions, and the third is the values. For example we can see the third row like this:
>> C(3,:)
ans =
'112.03' 'INU MAX POWER' '9000 kW'
If you want to convert the values to numeric, then you can easily do this in one step using my FEX submission sinum, which converts string like '9000 kW' to the numeric value of 9000000 plus the unit string 'W'. You can use it simply like this:
[val,spl,sgf] = cellfun(@sinum,C(:,3),'UniformOutput',false);
Again looking at the third row, we already have its value and unit:
>> val{3}
ans =
9000000
>> spl{3}{2}
ans =
'W'
This code works correctly on the following data file:
6 个评论
Stephen23
2015-3-6
编辑:Stephen23
2015-3-6
You can easily select a subset of your data that matches any conditions that you want. If you want to select all values starting with '150', then using strncmp gives the indices of those rows:
idx = strncmp(C(:,1),'150',3)
You can use this to extract just those rows:
C(idx,:)
and
val(idx)
etc.
You could even concatenate those numeric values into a single numeric array like this:
[val{idx}]
更多回答(2 个)
Guillaume
2015-3-2
编辑:Guillaume
2015-3-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 个评论
Guillaume
2015-3-4
编辑:Guillaume
2015-3-4
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?
Guillaume
2015-3-4
编辑:Guillaume
2015-3-4
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
2 个评论
Guillaume
2015-3-4
Typical reason for this is the function is not in your matlab path. It certainly has nothing to do with my code.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!