dlmread vs load vs fopen. When to use each of them ?

60 views (last 30 days)
I am stil begineer beginner and I use octave for coding and I don't understand the difference between dlmread/load/fopen. Sometimes I found that when to read a txt file they use dlmread sometimes or load other times. So can anyone please explain simply what is the difference between them ?

Answers (1)

Image Analyst
Image Analyst on 9 Jun 2023
load is normally used to open .MAT format files, though it's smart enough to open other formats if the data is laid out simply enough. save() and load() can handle any kind of MATLAB variables, even structures, tables, cell arrays, etc. that other input/output functions can't handle.
csvread is to read tables of numbers where numbers on a row are separated by commas.
dlmread is like csv read but it can handle other delimiters, such as tab and space, not just commas.
The last two are now recommended to be replaced by readmatrix (for all numbers) or readcell (if lines have a mixture of numbers and text on them. They are generally used to read a single variable saved in a file, unlike load() which can read in any number of totally different variables.
You might also want to consider importdata. It's more flexible than dlmread, and has some intelligence built in to figure out what kind of data is there in the file, and how to parse it and give it back to you.
fopen, fgetl, and fread are for lower level reading of files. fread can handle binary files, while fgetl is for text files. With these you can handle any format of input file because you basically have to tell it exactly what to get from the file and then you have to parse it manually. But they are the most flexible if you have a non-standard format.

Community Treasure Hunt

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

Start Hunting!