Read Text File in Matlab
3 次查看(过去 30 天)
显示 更早的评论
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
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.
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!