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 件のコメント
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
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
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 件のコメント
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
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
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
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);
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
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
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
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);
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
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
2020 年 5 月 26 日
fname = fullfile(S(k).folder,S(k).name);
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
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
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 で Data Import and Export についてさらに検索
タグ
タグが未入力です。
参考
2020 年 5 月 25 日
2020 年 5 月 26 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
