enumeration Fails with get(), set() arguments

조회 수: 5(최근 30일)
dpb
dpb 2021년 5월 31일
답변: dpb 2021년 12월 31일
classdef XlBordersIndex < int32
enumeration
xlDiagonalDown (5)
xlDiagonalUp (6)
xlEdgeLeft (7)
xlEdgeTop (8)
xlEdgeBottom (9)
xlEdgeRight (10)
xlInsideVertical (11)
xlInsideHorizontal (12)
end
end
Sample consumer routine
function SetBorder(sheetReference, ranges, border, weight, style)
% Usage
% SetBorder(WorksheetObject, ranges, borderIndex, borderWeight, lineStyle)
%
% sets the worksheet cell ranges specific border to the weight and style specified
% ranges is a char string, cellstr, or array of cellstr or string ranges
% borderIndex, borderWeight and lineStyle are scalar values
if ischar(ranges), ranges=cellstr(ranges); end
for i=1:numel(ranges)
try
theCell=sheetReference.Range(ranges{i});
borders=get(theCell, 'Borders');
thisBorder=get(borders, 'Item', border);
set(thisBorder,'LineStyle', style,'Weight',weight);
catch ME
fprintf('Error in function SetBorder.\nError Message:\n%s\n', ME.message)
%warning('Error in function SetBorder.\n%s\n', ME.message)
end
end
end
called as
excel=actxserver('Excel.Application');
wbk=excel.Workbooks.Open(outputFile);
wksht=Excel_utils.sheetReference(excel,outputSheet);
% draw border above totals row to show summations, payouts
ixTotal=find(matches(cBill(:,matches(cBill(1,:),'Scholarship')),'Billing Total'));
ixCols=find(matches(cBill(1,:),{'Student','Scholarship','Paid'}));
addr=[cellstr(join(string([arrayfun(@(r) xlsAddr(r,ixCols(1)),ixTotal,'UniformOutput',false), ...
arrayfun(@(r) xlsAddr(r,ixCols(2)),ixTotal,'UniformOutput',false)]),':')); ...
arrayfun(@(r) xlsAddr(r,ixCols(3)),ixTotal,'UniformOutput',false)];
Excel_utils.SetBorder(wksht,addr,XlBordersIndex.xlEdgeTop,XlBorderWeight.xlMedium, ...
XlLineStyle.xlContinuous);
results in
errorMessage =
'Error in function SetBorder.
Error Message:
Error: Object returned error code: 0x800A03EC'
If one inserts
if isenum(border), border=int32(border); end
if isenum(weight), weight=int32(weight); end
before the loop, the function runs without error and successfully changes the desired cell range borders to style and type requested.
This seems to pretty-much negate the whole point of an enumeration -- is there any cleaner workaround?
Looks to me like a bug although I suppose since is external interface they can claim "unsupported" usage.
  댓글 수: 1
dpb
dpb 2021년 5월 31일
Oh. There's a secondary Q? here as well -- one will note the peculiar code in the catch clause of
catch ME
fprintf('Error in function SetBorder.\nError Message:\n%s\n', ME.message)
%warning('Error in function SetBorder.\n%s\n', ME.message)
end
The initial use of the warning() statement on its own caused me to not discover the above error for some time when looking at the resulting spreadsheet instead of letting me know when was created -- why is no warning message shown if write
catch ME
msg=sprintf('Error in function SetBorder.\nError Message:\n%s\n', ME.message);
warning(msg)
end
or the version with the formatting string inside warning() as the nag screen would have you?
Hence, just the expedient of fprintf w/o the trailing semicolon so see the message.

댓글을 달려면 로그인하십시오.

답변(1개)

dpb
dpb 2021년 12월 31일
OK, I (finally!) figured out how to code the above to use a named constant with external interface -- don't use MATLAB enumeration but declare the constants as (Constant) in a class instead. ML enumerations are only useful inside MATLAB code that can and does interpret what an enumeration object is; don't try to use it elsewhere.
A sample class that does work is
classdef XlBorderWeight
properties (Constant)
xlHairline = 1 % Hairline (thinnest border).
xlMedium =-4138 % Medium.
xlThick = 4 % Thick (widest border).
xlThin = 2 % Thin.
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by