open a text file using fopen in read mode

22 visualizzazioni (ultimi 30 giorni)
ShimShim
ShimShim il 11 Lug 2018
Modificato: dpb il 12 Lug 2018
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?

Risposte (2)

Rik
Rik il 11 Lug 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.

dpb
dpb il 11 Lug 2018
Modificato: dpb il 12 Lug 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.

Community Treasure Hunt

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

Start Hunting!

Translated by