How to figure out the number of objects in a workspace?

조회 수: 15 (최근 30일)
Stagleton
Stagleton 2011년 8월 9일
I have taken the workspace function whos and assigned it to variable obj. How do I then find the number of objects in the workspace?
I have:
obj = evalin('base','whos');
%then I go into a loop
count=0;
while(true)
count=count+1;
something=obj(count).class;
%do something
end
Is there a way to find length or sizof obj.class?

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 8월 9일
a=whos;
nObj = size({a(:).size},2);
clear a; %to keep the same number of objects; also assuming a _wasn't_ a variable before.
Thus instead of calling it 'a', I would probably call it something like:
hjsdkjfdlsw6237
or
['dklsfjldrf' dec2hex(floor(rand*1000000))]
  댓글 수: 2
Stagleton
Stagleton 2011년 8월 9일
That works well, but I in addition to clearing a I think also subtracting 1 is important like Arnaud suggests.
Sean de Wolski
Sean de Wolski 2011년 8월 9일
That part isn't clear to me:
clear
a = whos;
length({a.size})
ans =
0
I'd think you might want to add one, since now there is a variable (_a_), actually in this case two variables (_a,ans_)

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

추가 답변 (1개)

Arnaud Miege
Arnaud Miege 2011년 8월 9일
How about length(obj)? Note that this will include obj itself, so you may want to subtract 1.
Arnaud
  댓글 수: 2
Stagleton
Stagleton 2011년 8월 9일
hmm, that doesn't work for me.
If I have:
obj=whos;
Then how do I find the number of objects in obj.class?
Arnaud Miege
Arnaud Miege 2011년 8월 9일
I don't think you need obj.class. length(obj) with give you the number of object in the workspace + obj itself.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by