Read Text File in Matlab
显示 更早的评论
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 个评论
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 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.
采纳的回答
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 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
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
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.
@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);
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.
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.
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.
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);
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);
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);
fname = fullfile(S(k).folder,S(k).name);
@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.
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.
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 中查找有关 Text Files 的更多信息
标签
尚未输入任何标签。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
