MATLAB Answers

Reading text file with number and strings

80 ビュー (過去 30 日間)
adi kul
adi kul 2018 年 3 月 6 日
コメント済み: adi kul 2018 年 3 月 6 日
Hello All, I am working on a script where I am requesting user to select file and provide inputs. Here is the code I am using:
[FileName2,PathName2] = uigetfile({'*.txt';'*.csv';'*.*'},'Select the text file that contains inputs parameters');
if isempty(FileName2)
errordlg('No file was selected that contains the data is required format.Hence the calculation was aborted.');
return
end
fid2 = fopen(fullfile(PathName2,FileName2));
if fid2 == -1
errordlg('The file selected for the inputs could not be read so the calculation was aborted.');
return
end
in_data = textscan(fid2,'%f%s');
fclose(fid2);
The error I am facing now is the data type I am reading.
Here is the sample "input.txt":
-9.11 #1st input
8.23 #2nd input
23000 #3rd input
0 #4th input
4 #5th input
Now the problem is , the input file will always have 1st column as numbers which I am interested in. and I just want to ignore the next columns. But with my above code, matlab reads only 1st number. If I remove all comments, it reads all numbers. So i want help to ignore all other columns and just read 1st column.

採用された回答

per isakson
per isakson 2018 年 3 月 6 日
編集済み: per isakson 2018 年 3 月 6 日
To skip the second column replace
in_data = textscan(fid2,'%f%s');
by
in_data = textscan(fid2,'%f%*s');
  1 件のコメント
adi kul
adi kul 2018 年 3 月 6 日
Thanks :)

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by