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일

0 개 추천

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

Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 25일
@Rik I am really grateful to you. I was searching for the answer since last three to four days. You really make my day and stay blessed. It works perfectly
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
편집: Rik 2020년 5월 26일
I want matlab to read multiple text file and same all the results (from a specific line) in all_results=[];. I have attached the text files in the offset folder as a reference. But when I execute it, it generates an error. Kindly check the code once.
EDIdir = 'E:\Result\fooof\CE\slope\offset\';
S = dir(fullfile(EDIdir,'*.txt'));
all_results=[];
% out = cell(size(S));
for k = 1:numel(S)
fname = S(k).name;
%read the file to a cell array (1 line per cell)
data=readfile(S);
%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}';
all_results1=val;
all_results=[all_results;all_results1];
end
Rik
Rik 2020년 5월 26일
I'm not on a computer right now, so can you apply correct formatting to your code and can you describe what error you are getting? I don't see any obvious mistakes.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
@Rik It generates error in line 8 as:
Error in readfile (line 72)
error('HJW:readfile:ReadFail',['%s could not read the file %s.',...
Error in alltext (line 8)
data=readfile(S);
Rik
Rik 2020년 5월 26일
편집: Rik 2020년 5월 26일
What does it display as filename? Because this line should display a readable error. If you are not sure, clear the command window with clc, run your code, and copy all red text.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
File name is the attached text files as ch1, ch2 and ch3. I want matlab to read these files and generates the output according to the command.
Rik
Rik 2020년 5월 26일
Copy all the red text.
An internal Matlab function is not able to read your file. That means the location of the files doesn't allow reading of the file, or your code generates an incorrect file name. I am not asking this to be annoying, but because I need to know what is going on exactly. If you don't provide me with the information I'm asking for I will not be able to help you.
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
All red text are copied as:
Error using error
Function is not defined for 'struct' inputs.
Error in readfile (line 72)
error('HJW:readfile:ReadFail',['%s could not read the file %s.',...
Error in alltext (line 8)
data=readfile(S);
Rik
Rik 2020년 5월 26일
편집: Rik 2020년 5월 26일
I will update the readfile function to return an error for such an input.
Look at your code:
fname = S(k).name;
%read the file to a cell array (1 line per cell)
data=readfile(S);
If you have this code in a function, you will see an orange line below fname, indicating a warning by mlint. If you look at the warning you see that Matlab thinks this variable is unused. That is obviously not what should be happening. You are generating a file name, but you aren't using it. You should change the second line to
data=readfile(fname);
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
I have modified the code and still it is generating error as:
Error using readfile (line 72)
Matlab could not read the file ch1.txt.
The file doesn't exist or is not readable.
Error in alltext (line 8)
data=readfile(fname);
Rik
Rik 2020년 5월 26일
fname = fullfile(S(k).folder,S(k).name);
Khan Muhammad Adeel Khan
Khan Muhammad Adeel Khan 2020년 5월 26일
@Rik I am extremely grateful for your swift response and sorry to bother you. Can you please recommend me best tutorial to learn matlab coding effectively. I can do the coding according to my requirement but want to learn it effectively. thanking you in anticipation.
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개)

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

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

Community Treasure Hunt

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

Start Hunting!

Translated by