How to read from a text file after skipping few characters in the file ?
2 views (last 30 days)
Show older comments
Suppose I have a file like this :
ID: 100 Strength: 800e+001 Time: 4536.83736
I want to read only the numerical values. How can I do this ?
4 Comments
Stephen23
on 2 Mar 2015
Edited: Stephen23
on 2 Mar 2015
- the supplied filename is not the name of an actual file.
- the file is not located in the current directory and you have not provided the fullpath.
You don't tell us anything about how your files are arranged so it is impossible for me to say which of these are the problem. However you can fix it quite easily yourself, by providing the correct filename/filpath as the first argument to fopen:
fid = fopen(filename,'rt'); % file is in current directory
If you need to generate the fullpath from path and filename strings, then you should use fullfile to do this:
fid = fopen(fullfile(pathstr,filename),'rt');
Answers (1)
Stephen23
on 2 Mar 2015
Edited: Stephen23
on 2 Mar 2015
Try this:
fid = fopen('temp.txt','rt');
C = textscan(fid,'%*s%f%*s%f%*s%f');
fclose(fid);
C{1} will contain the first column of numeric values, C{2} contains the second column, and C{3} contains the third. If you also decide that you want the field names, simply remove the asterisks from the format string.
A slightly more advanced solution is to keep the fieldnames, and turn them into a structure:
fid = fopen('temp.txt','rt');
C = textscan(fid,'%s%f%s%f%s%f');
fclose(fid);
C(1:2:end) = strrep(cellfun(@unique,C(1:2:end)),':','');
D = struct(C{:});
D.Strength
With the addition of
C(2:2:end) = cellfun(@num2cell,C(2:2:end),'UniformOutput',false);
it could also be turned into a non-scalar structure, which might make your work even easier.
2 Comments
Stephen23
on 3 Mar 2015
Edited: Stephen23
on 3 Mar 2015
Thank you for providing the complete textfile: as we can't read minds or your computer screen, we need you to help us by giving as much information as possible so that we can actually know what you need to do. There are billions of possible "textfile" formats and ways of arranging and accessing data, and as you can see, what you wrote and what you have in the file are different. Keep that in mind when you ask questions in future.
Now that we have your actual file, it was easy to create the correct code to read the data using textscan into a cell array C:
fid = fopen('current.txt','rt');
C = textscan(fid,'%s%f', 'MultipleDelimsAsOne',true,...
'HeaderLines',1, 'Delimiter',':');
fclose(fid);
You can access the variable names (strings) in C{1}, ans the numeric data are stored in C{2}. We can access these values using indexing:
>> C{1}{3}
ans =
Eccentricity
>> C{2}(3)
ans =
0.0039239
Note that to get the string we use cell array indexing with curly braces {}, whereas to get the numeric values we must use array indexing with parenthses ().
See Also
Categories
Find more on Text Files 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!