필터 지우기
필터 지우기

Read Text File in Matlab

조회 수: 7 (최근 30일)
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 25일
댓글: Khan Muhammad Adeel Khan 2020년 5월 26일
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
Rik
Rik 2020년 5월 25일
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 2020년 5월 25일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 5월 25일
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
Rik
Rik 2020년 5월 26일
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 2020년 5월 26일
So nice of you and I am extremely grateful. Stay blessed always and thanks for making this platform very friendly and productive.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by