File Exchange

image thumbnail

convertxlrange

version 1.0.0.0 (5.76 KB) by Kirby Fears
Converts Excel ranges to row and column boundary values

1 Download

Updated 09 Aug 2015

View License

This function converts Excel ranges into Matlab-usable format. It can also be used to test if a string is a valid A1-style Excel range.
Below are the description, syntax, and examples you will find in the m-file.
% Function Description
% This function converts excel A1 range strings such as 'A5:B10' into row
% and column pairs that convey the same information in Matlab-friendly
% row and column boundaries. For example, 'A5:B10' is converted to
% outrange.rows=[5 10]
% outrange.cols=[1 2]
%
% This allows for simple extraction of the desired region from cell or
% numerical arrays.
%
% Copyright Kirby Fears, 2015
%
%
% Function Call
% function outrange = convertxlrange(rangestr)
%
% Input: rangestr should be a character string representing a valid A1
% Excel range.
%
% Output: outrange is a struct with outrange.status = bool
% outrange.rows = [startrow endrow]
% outrange.cols = [startcol endcol]
%
% Empty matrix can be returned for rows or cols independently.
% The outrange.status is true if a valid A1 range is provided in
% rangestr and false otherwise.
% If an invalid Excel range is provided, outrange.rows and
% outrange.cols are both empty matrices.
% If the start row (or col) is greater than the end row (or col),
% the rows and columns are returned as is (not necessarily
% empty), and the status is returned as false.
%
% Examples:
% outrange=convertxlrange('A1:B2');
% status: 1
% rows: [1 2]
% cols: [1 2]
%
% Using these outputs, the desired parts of an array can be selected as follows:
% selection=samplearray(outrange.rows(1):outrange.rows(2),...
% outrange.cols(1):outrange.cols(2));
%
% outrange=convertxlrange('A:BC');
% status: 1
% rows: []
% cols: [1 55]
%
% outrange=convertxlrange('5:25');
% status: 1
% rows: [5 25]
% cols: []
%
% outrange = convertxlrange('Oh:my');
% status: 0
% rows: []
% cols: [398 363]

Cite As

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

Comments and Ratings (0)

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!