Using a loop with a cell array

I have a big data set in excel. About 120,000 rows by 23 columns. I have used the read command to input this to Matlab and now want to go through each row in column 10 to search for user defined as unknown. Once it finds the unknown I want to delete the entire row. I'm new to Matlab and would appreciate the help. Here's what I've got:
>DataClean_ParkRun
>clc
>clear all
>[~,~,everything]=xlsread('File Wk1edited_Original.xlsx ');
>n=size(everything,1);
>b='unknown';
>for k=1:1:n;
> A{k}=everything{k,10};
> if A{k}==b;
> everything{k,:}=[];
> end
>end

回答 (1 件)

dpb
dpb 2017 年 6 月 11 日
編集済み: dpb 2017 年 6 月 12 日

0 投票

[~,~,raw]=xlsread('File Wk1edited_Original.xlsx ');
ix=cellfun(@isempty,strfind(raw(:,10),'unknown'));
raw(~ix,:)=[]; % get rid of those that are _not_ empty (do contain 'unknown')

2 件のコメント

Ruairí Quinlan
Ruairí Quinlan 2017 年 6 月 12 日
Thanks for the help but back to the drawing board I'm afraid. That returned a new array of 1's and 0's. Thanks though
dpb
dpb 2017 年 6 月 12 日
Well, ix is a logical vector where the the string is/isn't.
Oooh....I see I didn't get the negation operation in on the deletion row, however; the rows you want to remove are those that do not contain the 'unknown' string; that's the ones we eliminated above.
Use
raw(~ix)=[]; % instead
I think I recall I started to write
raw=raw(ix,:);
and then switched to delete instead of copy and didn't swap the logical. I'll update the Answer to coincide.
If there's still an issue, attach a short section of the file we can see...

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2017 年 6 月 11 日

編集済み:

dpb
2017 年 6 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by