Matlab strcmp Error - help

3 次查看(过去 30 天)
Prathamesh Halagi
Prathamesh Halagi 2020-12-24
评论: dpb 2020-12-25
Hello,
I am trying to write a code where I need to export a table from excel, later I want to get the value in column named 'IR' if the value in column 'Current' is less than -70.00. I did use strcmp earlier in other places where it worked well but it shows error when I use it for this part. The follwing is my script:
clear
clc
opts = detectImportOptions('DCIR2.xls', 'Sheet','sheet2','Range', 'A1:G21');
opts.SelectedVariableNames = opts.SelectedVariableNames([3, 5, 7]);
Table_3 = readtable('DCIR2.xls', opts);
DCIR = Table_3{strcmp(Table_3{:, 'Current'} '< -135'), 'IR'};

采纳的回答

dpb
dpb 2020-12-24
The biggest problem is you're trying to deal with numeric data as string -- convert to numeric. This, unfortunately, isn't handled at all well by MATLAB out of the box when using European conventions as MATLAB ignores the user locale setting for numeric values. This is, in this day and age, truly embarrassing for TMW imo.
However, for simple cases like this, it's easy enough to work around...
>> load Table_3 % load the example table; see what looks like...
>> head(Table_3)
ans =
8×7 table
StepID StepID_1 Voltage Voltage_1 Current Current_1 IR
______________ ___________ __________ __________ ____________ _________ _________
{'2 CC_DChg' } {'3 Rest' } {'4,0057'} {'4,0848'} {'-135,993'} 0.00 {'0,582'}
{'5 CC_DChg' } {'6 Rest' } {'3,9434'} {'4,0042'} {'-67,969' } 0.00 {'0,895'}
{'7 CC_DChg' } {'8 Rest' } {'3,9124'} {'3,9955'} {'-135,956'} 0.00 {'0,611'}
{'10 CC_DChg'} {'11 Rest'} {'3,8581'} {'3,9245'} {'-67,987' } 0.00 {'0,977'}
{'12 CC_DChg'} {'13 Rest'} {'3,8284'} {'3,9155'} {'-136,012'} 0.00 {'0,64' }
{'15 CC_DChg'} {'16 Rest'} {'3,7701'} {'3,851' } {'-67,987' } 0.00 {'1,19' }
{'17 CC_DChg'} {'18 Rest'} {'3,7506'} {'3,8392'} {'-135,956'} 0.00 {'0,652'}
{'20 CC_DChg'} {'21 Rest'} {'3,6845'} {'3,7478'} {'-67,969' } 0.00 {'0,931'}
>>
>> Table_3.IR=str2double(strrep(Table_3.IR,',','.')); % convert the IR variable to numeric; swap '.' for ',' to do so
>> head(Table_3) % now see what that looks like...
ans =
8×7 table
StepID StepID_1 Voltage Voltage_1 Current Current_1 IR
______________ ___________ __________ __________ ____________ _________ ____
{'2 CC_DChg' } {'3 Rest' } {'4,0057'} {'4,0848'} {'-135,993'} 0.00 0.58
{'5 CC_DChg' } {'6 Rest' } {'3,9434'} {'4,0042'} {'-67,969' } 0.00 0.90
{'7 CC_DChg' } {'8 Rest' } {'3,9124'} {'3,9955'} {'-135,956'} 0.00 0.61
{'10 CC_DChg'} {'11 Rest'} {'3,8581'} {'3,9245'} {'-67,987' } 0.00 0.98
{'12 CC_DChg'} {'13 Rest'} {'3,8284'} {'3,9155'} {'-136,012'} 0.00 0.64
{'15 CC_DChg'} {'16 Rest'} {'3,7701'} {'3,851' } {'-67,987' } 0.00 1.19
{'17 CC_DChg'} {'18 Rest'} {'3,7506'} {'3,8392'} {'-135,956'} 0.00 0.65
{'20 CC_DChg'} {'21 Rest'} {'3,6845'} {'3,7478'} {'-67,969' } 0.00 0.93
>> Table_3.StepID=categorical(Table_3.StepID); % also, the ID variables are categorical...convert
>> head(Table_3)
ans =
8×7 table
StepID StepID_1 Voltage Voltage_1 Current Current_1 IR
__________ ___________ __________ __________ ____________ _________ ____
2 CC_DChg {'3 Rest' } {'4,0057'} {'4,0848'} {'-135,993'} 0.00 0.58
5 CC_DChg {'6 Rest' } {'3,9434'} {'4,0042'} {'-67,969' } 0.00 0.90
7 CC_DChg {'8 Rest' } {'3,9124'} {'3,9955'} {'-135,956'} 0.00 0.61
10 CC_DChg {'11 Rest'} {'3,8581'} {'3,9245'} {'-67,987' } 0.00 0.98
12 CC_DChg {'13 Rest'} {'3,8284'} {'3,9155'} {'-136,012'} 0.00 0.64
15 CC_DChg {'16 Rest'} {'3,7701'} {'3,851' } {'-67,987' } 0.00 1.19
17 CC_DChg {'18 Rest'} {'3,7506'} {'3,8392'} {'-135,956'} 0.00 0.65
20 CC_DChg {'21 Rest'} {'3,6845'} {'3,7478'} {'-67,969' } 0.00 0.93
>>
The rest should be apparent to finish cleaning up the table; then you can use the data as numeric and direct numeric comparisons for the logic tests. Text comparisons of straight numeric float strings won't work for relational numeric comparisons; that's doing a strict lexical comparison, not numeric.
There is a way to work around the MATLAB limitations in a more generic fashion but it takes more work up front -- see Yair Altman's undocumented site at <Formatting-numbers> for using the Java engine instead that is locale aware.
  7 个评论
Steven Lord
Steven Lord 2020-12-25
The biggest problem is you're trying to deal with numeric data as string -- convert to numeric. This, unfortunately, isn't handled at all well by MATLAB out of the box when using European conventions as MATLAB ignores the user locale setting for numeric values. This is, in this day and age, truly embarrassing for TMW imo.
If you're using detectImportOptions you can specify the 'DecimalSeparator' option either in the detectImportOptions call itself or using setvaropts after the import options object has been created. The 'ThousandsSeparator' option may also be of interest.
dpb
dpb 2020-12-25
Thanks for the heads-up, Steven...I had looked thinking should be there and somehow missed seeing it...which release incorporated the feature, you remember? (Not convenient to open ML at the moment).

请先登录,再进行评论。

更多回答(0 个)

产品

Community Treasure Hunt

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

Start Hunting!

Translated by