Matlab strcmp Error - help
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2020년 12월 24일
1 개 추천
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
Prathamesh Halagi
2020년 12월 25일
Unfortunately this does not help in the final ppart of getting the values extracted from IR if the value of CUrrent is < -70.
Walter Roberson
2020년 12월 25일
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
2020년 12월 25일
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.'
Walter Roberson
2020년 12월 25일
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
2020년 12월 25일
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
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
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개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
