Read Text File in Matlab

6 views (last 30 days)
How ca I read such text file in matlab:
Covariances:
Estimate Std.Err z-value P(>|z|) Std.lv Std.all
read ~~
read1 0.696 0.040 17.535 0.000 0.696 0.696
I want matlab to read (read1) line and it's line69 in my text file. I tried to read it via following command but unable to read it
match1 = regexp(file,'(?<\s\s\s\sfi\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s)(\d*.?\d*)(\s\s\s\s)(\d*.?\d*)(\s\s\s\s)(\d*.?\d*)(\s\s\s\s)(\d*.?\d*)(\s\s\s\s)(\d*.?\d*)','match');
Kindly guide me
  2 Comments
Rik
Rik on 25 May 2020
Is there a specific reason you are trying to use such a complex regular expression?
It would be best if you attach an example text file.
A last note: tagging a person doesn't notify them, so there is no need to do so. I have removed them, since they do not describe your question.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan on 25 May 2020
@Rik I want to matlab to read the line 69 in the attached text file. Thanks for the swift response as I am newbie here so don't know the complete procedure to use this platform effectively.

Sign in to comment.

Accepted Answer

Rik
Rik on 25 May 2020
There are many ways to do this. One of the many ways is using the code below. It makes use of the readfile function which you can find here on the FEX. It reads a file to a cell array.
%read the file to a cell array (1 line per cell)
data=readfile('ch1.txt');
%find relevant line
line=find(ismember(data,{'Covariances:'}));
line=line+3;%go to the line with the data
str=data{line};
%trim leading spaces
str=strtrim(str);
%remove header
spacepos=strfind(str,' ');str(1:spacepos(1))='';
%convert to values
val=textscan(str,'%f');val=val{1};
  14 Comments
Rik
Rik on 26 May 2020
You're welcome. You aren't bothering me, if you were I would probably just ignore this thread. (although to be clear: not responding could also mean I have little time or I missed a post)
I can recommend the Onramp course provided by Mathworks. It should teach you the basics.
The reason for the error in this case is the path. If you don't specify a complete path to your file, Matlab will look in your current folder and then in the installation folders of Matlab itself. That is true for function calls, but also files you are trying to read.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan on 26 May 2020
So nice of you and I am extremely grateful. Stay blessed always and thanks for making this platform very friendly and productive.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!