関数 XLSREAD/XLSWRITE において EXCEL のデータ領域を行と列​の数字で指定すること​はできますか。

XLSREAD (XLSWRITE) を用いてデータの読み込み(書き込み)を行いたいと考えています。データ領域を行と列の数字で、例えば A2 を (1,2) のように指定する方法を教えてください。

 採用された回答

MathWorks Support Team
MathWorks Support Team 2013 年 10 月 25 日

0 投票

関数 XLSREAD/XLSWRITE においてデータ領域は、「A1:A2」のようにアルファベットを用いた形式で指定しなければなりません。
そのため、数値からセルの名前に対応するアルファベットに変換すうようなプログラムを作成する必要があります。下記の関数 excelName は、その一例です。
function cellName = excelName(row, col)
% Generates excel cell name from numeric (row, col) index.
% The following assertions ensure valid generation of cell names
% for excel versions that requires to have rows between 1 and 65536,
% and columns between 1 and 256.
assert(((row > 0) && (row <= 65536)),...
'Row index must be a positive integer not exceeding 65536.');
assert(((col > 0) && (col <= 256)),...
'Column index must be a positive integer not exceeding 256.');
% Find the alphabetic name and append with row number.
cellName = strcat(cellAlphabet(col), num2str(row));
function alphabetOut = cellAlphabet(col)
% Helper function for generating alphabetic name.
% Used moduler arithmetic to determine number of alphabets in the name.
% Assigned A, B, ..., Z for 0, 1, ..., 25 respectively.
quotient = floor((col-1)/26);
remainder = mod(col-1, 26);
if quotient <= 0
str = char(remainder + 65);
elseif quotient < 26
str = char(quotient - 1 + 65);
str = strcat(str, char(remainder + 65));
else
str = strcat(cellAlphabet(quotient),char(remainder + 65));
end
alphabetOut = str;
end
end
excelName 関数は、以下のように使用することができます。
% A1:C8 のデータを取り込む例
>> xlsread('test.xlsx', strcat(excelName(1,1),':', excelName(8,3)))

その他の回答 (0 件)

製品

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!