using attribute 'size' in validateattributes

4 ビュー (過去 30 日間)
Sina Shojaei
Sina Shojaei 2015 年 2 月 5 日
コメント済み: Sina Shojaei 2015 年 9 月 8 日
Hi,Any idea how I can pass size of a matrix to validateattributes? I need to check that size(A,2) < 2, where A is the input to my function.
Would be appreciated.

採用された回答

David Young
David Young 2015 年 2 月 5 日
I'm pretty sure validateattributes can't do what you want.
If in fact A is required to be a column vector, you can use
validateattributes(A, {'numeric'}, {'column'}); % change class as appropriate
but if it's OK for A to be empty, or if it's possible for A to have 3 or more dimensions, then you need to write out the check in the conventional way:
if size(A,2) >= 2
error(...);
end
If you are using writing a checking function for inputParser, the answer is that it's possible to do what you want but a little more complex - please say if you need that.
  2 件のコメント
Adam
Adam 2015 年 2 月 6 日
Or if emptiness is the only other option you want to accept in addition to a column I tend to use:
if ~isempty( A )
validateattributes(A, {'numeric'}, {'column'}); % change class as appropriate
end
Sina Shojaei
Sina Shojaei 2015 年 9 月 8 日
Thanks

サインインしてコメントする。

その他の回答 (1 件)

Daniel Lyddy
Daniel Lyddy 2015 年 6 月 12 日
I think this might be what you want:
% set up validator
myValidMatrix = @(x) validateattributes(x, {'numeric'}, ...
{'real', '2d', 'size', [nan, 2]});
That nan in the 'size' vector is an "I don't care". If instead your array is 3D and you only care about the middle dimension, you would do:
myValidArray = @(x) validateattributes(x, {'numeric'}, ...
{'real', '3d', 'size', [nan, 2, nan]});
~Daniel

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by