convertxlrange
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 (2024). convertxlrange (https://www.mathworks.com/matlabcentral/fileexchange/52422-convertxlrange), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0.0 | Edited description. |