이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
If Statement Using Text in a Table
조회 수: 11 (최근 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)