필터 지우기
필터 지우기

isempty: Not enough input arguments.

조회 수: 10 (최근 30일)
Octavian
Octavian 2020년 3월 19일
답변: Steven Lord 2020년 3월 19일
Dear All,
I have an empty structure x.y.z
isfield(x.y, 'z') is logical 1, but
isempty(x.y.z) gives me this error:
Error using isempty
Not enough input arguments.
How to check that z is empty? Thank you,
OL
  댓글 수: 1
Adam Danz
Adam Danz 2020년 3월 19일
You shouldn't get that error.
x.y.z = [];
isfield(x.y,'z') % = true
isempty(x.y.z) % = true
Confirm that you're using Matlab's isempty function.
which isempty -all
The output should include a list of built-in functions/methods.

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

채택된 답변

Steven Lord
Steven Lord 2020년 3월 19일
The problem is not that the field z of x.y is empty. The problem is that the field y of x is. In the example below, due to the behavior of the struct constructor when one of the values of the fields is a cell array, x.y is a 0-by-0 struct array.
x.y = struct('z', {});
size(x.y) % [0 0]
When S is a struct array with a field T, S.T makes what's known as a comma-separated list with one element per element of S. Consider:
q.y = struct('z', 1);
{q.y.z} % {1}
w.y = struct('z', {2, 3:4});
{w.y.z} % {2, [3 4]}
As that documentation page states, when you create a comma-separated list you can pass it into a function as the input arguments, one input per element in the comma-separated list.
plus(w.y.z) % equivalent of plus(2, [3 4])
So when the struct from which you create the comma-separated list is empty, as it is if I were to create one from x.y, that's like calling the function with no inputs.
isempty(x.y.z) % equivalent of isempty()
isempty requires you to call it with an input.
Check that x.y has some data (is not empty, or better yet isscalar) before you start asking questions about its data.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2020년 3월 19일
Do you have an example data?
>> x.y.z=[]
x =
struct with fields:
y: [1×1 struct]
>> isempty(x.y.z)
ans =
logical
1

Image Analyst
Image Analyst 2020년 3월 19일
It should work. See this:
% Assign x.y.z to some value.
x.y.z = 10
tf = isempty(x.y.z) % Shows false
% Now make field z empty.
x.y.z = []; % Null, empty
tf = isempty(x.y.z) % Shows true
% Remove the field z
x.y = rmfield(x.y, 'z')
tf = isempty(x.y.z) % Throws error
Attach your data if you still have trouble.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by