If Statement Using Text in a Table
显示 更早的评论
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
采纳的回答
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 个评论
Thank you Madhan.
Can you please tell me why you have specified 'asdsa' and what it does in your code?
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.
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.
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
T=arr_1({'REMOVE'}, :);
So doesn’t the below lines do what you want??
idx=strcmp(arr_1{:,1},"REMOVE");
arr_1(idx,:) = [] % remove those rows
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.
"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:
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.
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))])
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.
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.
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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Simulink Functions 的更多信息
另请参阅
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)
