AAlejandroVR
AAlejandroVR

Reputation: 27

How to Compare the same Char Value in a Cell Array 1x1 with If Else Sentence

First i do my connection and assign the names for table, columns, rows...

conn = database('db', 'user', 'pass');

Then i make a query, for select in the data base an Varchar

curs = exec(conn, 'select row/column from table');

I request the Data

curs = fetch(curs);
AA = curs.Data;

then my problem begin... I'll add a variable with the name 'Alex', to find the same name in my database to the assigned column and use the if statement to verify their existence..

var = 'Alex';

then I wonder what is the best choice to retrieve the data and make a comparison between the database and my Variable:

index = find(strcmp(AA,var));

or

data = strfind(AA,var);

and my structure for the if statement is this:

if AA == var

  msgbox('Data Exist')

end

this doesn't work for me, i just receive "Undefined function 'eq' for input arguments of type 'cell'."

Upvotes: 0

Views: 257

Answers (2)

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

if AA is guaranteed to be a 1x1 cell array strcmp(var, AA{1}) or isequal({var}, AA)should work. However, if it can have more elements you can use ismember(var, AA).

Upvotes: 0

Stewie Griffin
Stewie Griffin

Reputation: 14939

So, I'm assuming you have something like:

var = 'Alex';
AA = {'Alex'};

Now, you can handle this in several ways, my favorite being:

isequal(var, AA{:})

The key point here is the {:} that extracts the content of the cell.

if var == AA{:} would work too, but only if var and AA{1} are the same length, otherwise it will cause an error. I would therefore not recommend using == for strings.

Upvotes: 1

Related Questions