How to use exist for a cell?

Hello,
I have a cell with string elements and I am trying to check if they exist as variables:
A={'B0','B01','B04'}
for i=1:3
if exist(A(i,1),'var')==1
...
This does not work so what do I do? Thx!

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Apr. 2012

1 Stimme

if exist(A{i,1},'var')
Notice the {} instead of ()

3 Kommentare

osminbas
osminbas am 12 Apr. 2012
You rock, Walter!
It doesn't work for me! Here's the code:
>> h = {1,2;3,4};
>> exist('h','var')
ans =
1
>> exist('h{1}','var')
ans =
0
>> exist('h{1,1}','var')
ans =
0
Am I making any mistake? If not, is there any other way to check existence of different elements in a cell array?
Thanks
Matt J
Matt J am 7 Dez. 2012
Bearbeitet: Matt J am 7 Dez. 2012
You have several errors. First, notice that Walter doesn't enclose the first argument to exist() in quotes. Secondly, your h{i,j} data are not relevant examples because they are not strings.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

RNTL
RNTL am 12 Apr. 2012

1 Stimme

ismember(A,who)

2 Kommentare

osminbas
osminbas am 12 Apr. 2012
Actually, I am really trying to use exist and check the elements one by one in a loop so ismember(A,who) would not work for me.
Jan
Jan am 7 Dez. 2012
@osminbas: Please take the time to think twice. This method would help to avoid the loop or at least compare all elements of the cell string h in a very compact form.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by