how to read the parameter and parameter value from the text file

10 views (last 30 days)
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

Stephen23
Stephen23 on 4 Mar 2015
Edited: Stephen23 on 4 Mar 2015
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 Comments
madhu T S
madhu T S on 6 Mar 2015
Edited: madhu T S on 6 Mar 2015
Hi.. Stephen.. sorry.. let me make it clear.... the txt file contains several ID numbers such as 110.01, 110.02, 120.01, 121.01... 150.01.. 150.05... 160.05 and so on... each ID hold a value.. now if I want to select/read only 150. series ID's and their values and save as .mat file... how can I save?? Hope its clear.. let me know if u dont understand
regards madhu
Stephen23
Stephen23 on 6 Mar 2015
Edited: Stephen23 on 6 Mar 2015
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}]
and then do whatever further processing you need. You can save to .MAT file using save.

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 2 Mar 2015
Edited: Guillaume on 2 Mar 2015
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
Guillaume
Guillaume on 4 Mar 2015
Edited: Guillaume on 4 Mar 2015
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?
madhu T S
madhu T S on 4 Mar 2015
Edited: madhu T S on 4 Mar 2015
hello Guilaume.. In the txt file there are num of blocks.. one among them is INVERTER DATA:
The num of entries, the text in the middle, format is fixed.. the value only changes for diff txt files and it may be floating number also(decimals).. when it is a string.. it should be stored as it is displaying..

Sign in to comment.


Guillaume
Guillaume on 4 Mar 2015
Edited: Guillaume on 4 Mar 2015
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 Comments
madhu T S
madhu T S on 4 Mar 2015
Hello Guilaume...
I tried to run the code given by u.. found error as shown below
??? Undefined function or method 'decode_file' for input arguments of type 'char'.
Error in ==> example at 187
parsed_data = decode_file(filename);
Guillaume
Guillaume on 4 Mar 2015
Typical reason for this is the function is not in your matlab path. It certainly has nothing to do with my code.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!