Clear Filters
Clear Filters

Access Table rows if variable value == ... Problems with string variables.

2 views (last 30 days)
Dear All, suppose you have a table. I want to access certain rows based on variables values, for example if datecode == 20001231 & country_code == 'ES'. The following code works for numerical variables, but not for strings:
test = EBADATA_DEFINITIONS(EBADATA_DEFINITIONS.MATURITY_CODE == 999 ...
& EBADATA_DEFINITIONS.INFORMATION_CODE == 34010 ...
& strcmp(EBADATA_DEFINITIONS.C_COUNTRY_CODE,'ES'),:);
I have tried to manipulate the country column in various ways before running the command above , but I always end up with one or another error.
EBADATA_DEFINITIONS.C_COUNTRY_CODE = char(EBADATA_DEFINITIONS.C_COUNTRY_CODE)
EBADATA_DEFINITIONS.C_COUNTRY_CODE = cell2str(EBADATA_DEFINITIONS.C_COUNTRY_CODE)
Could someone point me towards the correct way of doing this?
I appreciate your help!
Best!

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jun 2016
It seems to work for me:
t = cell2table({15, 134, 'hello'; 82, 419, 'there'; 15, -999, 'hello'}, 'VariableNames', {'MATURITY_CODE', 'INFORMATION_CODE', 'C_COUNTRY_CODE'});
t(t.MATURITY_CODE == 15 & strcmp(t.C_COUNTRY_CODE, 'hello'),:)
  1 Comment
roblocks
roblocks on 29 Jun 2016
it works for me now as well after not-charing it. I must have forgotten to unchar when I tested the code and then posted. Thanks Walter!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!