You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
If Statement Using Text in a Table
6 views (last 30 days)
Show older comments
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
Accepted Answer
madhan ravi
on 21 Jul 2019
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 Comments
madhan ravi
on 21 Jul 2019
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
on 22 Jul 2019
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
Walter Roberson
on 22 Jul 2019
T=arr_1({'REMOVE'}, :);
madhan ravi
on 22 Jul 2019
So doesn’t the below lines do what you want??
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
Jay
on 22 Jul 2019
Edited: Jay
on 22 Jul 2019
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
on 22 Jul 2019
Edited: Stephen23
on 22 Jul 2019
"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
on 22 Jul 2019
Edited: Jay
on 22 Jul 2019
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
on 22 Jul 2019
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
on 22 Jul 2019
Edited: Jay
on 22 Jul 2019
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
on 22 Jul 2019
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.
Walter Roberson
on 23 Jul 2019
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)