open a text file using fopen in read mode
25 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Rik
on 11 Jul 2018
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 Comments
dpb
on 11 Jul 2018
Edited: dpb
on 12 Jul 2018
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 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!