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'};

 Akzeptierte Antwort

dpb
dpb am 24 Dez. 2020

1 Stimme

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 Kommentare

Prathamesh Halagi
Prathamesh Halagi am 25 Dez. 2020
Unfortunately this does not help in the final ppart of getting the values extracted from IR if the value of CUrrent is < -70.
Table_3.Current=str2double(strrep(Table_3.Current,',','.')); % convert the Current variable to numeric; swap '.' for ',' to do so
wanted_output = Table_3.IR(Table_3.Current < -70)
Prathamesh Halagi
Prathamesh Halagi am 25 Dez. 2020
Yes, I tried as you said but I am getting this error:
'To assign to or create a variable in a table, the number of rows must match the height of the table.'
You are getting that error on the command
Table_3.Current=str2double(strrep(Table_3.Current,',','.')); % convert the Current variable to numeric; swap '.' for ',' to do so
?
If so then try
temp = strrep(Table_3.Current,',','.');
size(temp)
ntemp = str2double(temp)
size(Table_3)
Table_3.Current = ntemp;
and show us the output
Prathamesh Halagi
Prathamesh Halagi am 25 Dez. 2020
I got the required results after just using this part, after the table file was extracted. It works well. Thanks a lot.
wanted_output = Table_3.IR(Table_3.Current < -70)
Steven Lord
Steven Lord am 25 Dez. 2020
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 am 25 Dez. 2020
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).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Gefragt:

am 24 Dez. 2020

Kommentiert:

dpb
am 25 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by