open a text file using fopen in read mode
25 次查看(过去 30 天)
显示 更早的评论
I want to use fopen command to open a .txt file, consisting of 4 columns and 100 rows, in read mode, and scan it using fscanf command, then plot it.
But, when I use the command data=fopen('data.txt','r'), it only reads the first value
Is it possible to use fopen to open a text file?
0 个评论
回答(2 个)
Rik
2018-7-11
As you can tell from the documentation for fopen, the output is not actually the data, but a file ID. You need a reading function to get to the actual data, as you can see from the included examples.
0 个评论
dpb
2018-7-11
编辑:dpb
2018-7-12
data=fopen('data.txt','r');
fopen doesn't return data; all it does is return a file handle for fscanf, |textscan{ and friends.
What you interpreted as a value is instead the file handle; >0 means a success; <0 failure. You need to do a
fclose all
to close all active and perhaps orphaned file handles.
For a file such as you described, there's absolutely no sense in using low-level i/o functions; use importdata or readtable or one of the other high-level functions. See data-import-and-analysis for tutorial info.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!