image thumbnail

delimread

version 1.0.0.0 (5.04 KB) by Kirby Fears
Easily read text and numerical data from any delimited text file (such as CSV).

1.1K Downloads

Updated 10 Aug 2015

View License

This function is intended to behave like xlsread on csv files, but works on any delimited text file. Unlike textscan or csvread, delimread is able to read both text and numerical data without requiring a format string. A range can be specified in Excel A1 format, or alternatively in start/end row and column values. The function is optimized to ignore unneeded data on the first read instead of dropping unneeded data after reading the full file. It also behaves consistently across all operating systems, unlike xlsread. Data can be returned in the following forms as specified in the function input:
'num': return only numerical data in a numerical matrix.
'text': return only text data in a cell array of strings.
'mixed': return a cell array with mixed numerical and text data.
'raw': same as 'mixed' except numerical data is formatted as char.
Below are the description, syntax, and examples you will find in the m-file.
% Function Description
% This function reads delimited text files. Like xlsread, a range can be
% specified in Excel A1 format. Ranges can also be row & column boundary
% pairs. The results are broken out into numerical only, text only, and
% raw output, in imitation of xlsread.
% This is generally much faster than xlsread work well across all
% operating systems.
%
% Motivation: in Matlab 2015a, xlsread 'basic' mode does not operate
% on csv files or any other non-Excel files that can be opened in Excel.
% On Linux or Mac systems, 'basic' mode is enforced exclusively.
% Therefore, this is a great replacement for xlsread on non-excel files
% to enable code to run correctly on all operating systems.
%
% Copyright Kirby Fears, 2015
%
% DEPENDENCY
% This function requires convertxlrange.m, which converts A1 excel ranges
% to row/column boundary pairs.
%
% Function Call
% outstruct=delimread(fpath,delim,outstyle)
% Reads document specified by fpath using specified delimiters.
% delim: should be specified as a string or cell array of strings with
% one delimiter per string.
% outstyle: should be a string or cell array of strings. Each string
% should be a string in {'num', 'text', 'mixed', 'raw'}.
% Data is initially parsed as strings, creating the 'raw' output.
% Anything that can be converted using str2double is returned in 'num'
% output. All raw outputs excluding those convertible to numbers are
% returned in 'text'. All outputs are returned in 'mixed' with numerical
% and char data mixed into one cell structure.
% outstruct: a struct containing each specified outstyle as
% outstruct.num, outstruct.text, etc.
%
% outstruct=delimread(fpath,delim,outstyle,xlrange)
% Reads portion of document specified by xlrange in Excel A1 format.
%
% outstruct=delimread(fpath,delim,outstyle,rpair,cpair)
% Reads file specified by rpair = [minrow maxrow] and
% cpair = [mincol maxcol].
% Both rpair and cpair must be specified if one is specified. You can use
% blank matrix [] to read all rows or all columns. If a single row or
% column is to be read, it can be specified as a scalar instead of a
% min/max pair.

Cite As

Kirby Fears (2021). delimread (https://www.mathworks.com/matlabcentral/fileexchange/52423-delimread), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!