現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
If Statement Using Text in a Table
5 ビュー (過去 30 日間)
古いコメントを表示
What commands can I use in the following if statement to specify blank element overwrite?
% Run throught the table of 10 rows
for i = 1:10
% The word "REMOVE" has been used in a previous condition subroutine to specify the row removal in this if statment
if strcmp("REMOVE",table1(i,1)) == 1
% Emptys rows
table1(i,:) = []
else
end
end
The subroutine is executed and the table rows are not being emptied when true
採用された回答
madhan ravi
2019 年 7 月 21 日
I have no idea why you need a loop ( https://in.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html ):
T=table({'asdsa';'REMOVE'}); % example
idx=strcmp(T{:,1},"REMOVE");
T(idx,:) = [] % remove those rows
14 件のコメント
Jay
2019 年 7 月 21 日
Thank you Madhan.
Can you please tell me why you have specified 'asdsa' and what it does in your code?
Jay
2019 年 7 月 21 日
I get the following error returned:
Subscripting a table using linear indexing (one subscript) or multidimensionalindexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript.
madhan ravi
2019 年 7 月 21 日
It was just an example. Show the code that your using. Which version of MATLAB are you using? Upload your table as .mat file. Also determine which variable of the table you want to work on.
Jay
2019 年 7 月 22 日
clear
clc
%% INPUT
arr_1 = cell(10,4)
% Names
stringVals = {"Test 1", "Test 2", "REMOVE", "Test 4", "Test 5"}'
% Populate names
arr_1 = repmat(stringVals(1:5,1),2,1)
% Populate columns 2:4 with rand
for i =2:4
for j = 1:size(arr_1(:,1))
arr_1(j,i) = num2cell(randn)
end
end
% Convert to table
arr_1 = cell2table(arr_1)
%% Madhan's Code
T=arr_1({'REMOVE'}); % example
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
madhan ravi
2019 年 7 月 22 日
So doesn’t the below lines do what you want??
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
Jay
2019 年 7 月 22 日
編集済み: Jay
2019 年 7 月 22 日
I get the following error is thrown when executing:
T=arr_1({'REMOVE'},:); % example
Unrecognized row name 'REMOVE'.
The code as it stands is (in case I am missing something):
clear
clc
%% INPUT
arr_1 = cell(10,4)
% Names
stringVals = {"Test 1", "Test 2", "REMOVE", "Test 4", "Test 5"}'
% Populate names
arr_1 = repmat(stringVals(1:5,1),2,1)
% Populate columns 2:4 with rand
for i =2:4
for j = 1:size(arr_1(:,1))
arr_1(j,i) = num2cell(randn)
end
end
% Convert to table
arr_1 = cell2table(arr_1)
%% Madhan's Code
T=arr_1({'REMOVE'},:); % example
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
The code does not remove the "REMOVE" rows.
Stephen23
2019 年 7 月 22 日
編集済み: Stephen23
2019 年 7 月 22 日
"I get the following error ...Unrecognized row name 'REMOVE'."
Get rid of the T=... line.
Using loops like that to generate the table is very complex and inefficient. Simpler:
>> T = array2table(randn(10,3),'VariableNames',{'A','B','C'});
>> T.names = repmat({'Test 1';'Test 2';'REMOVE';'Test 4';'Test 5'},2,1)
T =
A B C names
_________ ________ ________ ________
-0.62909 -1.3981 0.88095 'Test 1'
-1.2038 -0.25506 0.32321 'Test 2'
-0.25394 0.1644 -0.78415 'REMOVE'
-1.4286 0.74773 -1.8054 'Test 4'
-0.020858 -0.27305 1.8586 'Test 5'
-0.56066 1.5763 -0.60453 'Test 1'
2.1778 -0.48094 0.10336 'Test 2'
1.1385 0.32751 0.56317 'REMOVE'
-2.4969 0.66473 0.1136 'Test 4'
0.44133 0.085189 -0.90473 'Test 5'
>> X = strcmp(T.names,'REMOVE');
>> T(X,:) = [] % remove those rows
T =
A B C names
_________ ________ ________ ________
-0.62909 -1.3981 0.88095 'Test 1'
-1.2038 -0.25506 0.32321 'Test 2'
-1.4286 0.74773 -1.8054 'Test 4'
-0.020858 -0.27305 1.8586 'Test 5'
-0.56066 1.5763 -0.60453 'Test 1'
2.1778 -0.48094 0.10336 'Test 2'
-2.4969 0.66473 0.1136 'Test 4'
0.44133 0.085189 -0.90473 'Test 5'
See also:
Jay
2019 年 7 月 22 日
編集済み: Jay
2019 年 7 月 22 日
I found an older answer deleting all rows from a table that contain a string you wrote madhan which fixed the problem.
Command used is now :
arr_1(~any(strcmp(arr_1{:,:},"REMOVE"),2),:)
I wish I came across it sooner.
Thank you for your help.
Guillaume
2019 年 7 月 22 日
With the example you provide,
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
does remove the unwanted rows and I can't see why it wouldn't do so for you. This syntax has always been valid for tables.
On the other hand, since you're using strings and not char vectors, you can use simply:
toremove = arr_1{:, 1} == "REMOVE";
arr_1(toremove, :) = []
This is equivalent to Madhan's code.
As Madhan said, there's usually no reason to use loops with tables. The way you construct your demo table is very inefficient. You could have done:
stringVals = {"Test 1", "Test 2", "REMOVE", "Test 4", "Test 5"}';
arr_1 = cell2table([repmat(stringVals, 2, 1), num2cell(randn(2*numel(stringVals), 3))])
Jay
2019 年 7 月 22 日
編集済み: Jay
2019 年 7 月 22 日
Hi Stephen,
The code listed is to demonstrate what I am trying to achieve for a section of a reduction program.
For the purpose of the program I am writing, the names in the first column will differ and having to "hard code" the values would be to inefficient in the larger scheme of things (and potentially lead to more errors).
My exposure to MATLAB (at university) has been using if and for statements and reading in an excel spreadsheet.
I am coding to streamline the reductions required for another unit.
I have only been introduced to tables and cells (along with their syntax requirements for a week or so) so still learning.
Guillaume,
You are correct, but only when the
%T=arr_1({'REMOVE'},:); % example
has been removed from the code.
I only found that out when you stated that the code works and commented out the first of 3 additional commands (under the "% Madhan's Code).
Thank you for your time and effort Walter, Stephan and Guillaume.
Walter Roberson
2019 年 7 月 22 日
T=arr_1({'REMOVE'}, :);
is for the case where you have specified a RowNames property for the table, which you have not done so. Without a RowNames property, the {'REMOVE'} indexing would not know which of the table fields to match against.
Jay
2019 年 7 月 23 日
Thank you for explaining what that command does Walter.
The explanations by the more advanced contributors help a lot more in understanding commands that exceed the simpler commands in the Matlab documentation.
Walter Roberson
2019 年 7 月 23 日
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)