Matlab strcmp Error - help
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
