Accessing cell array via factor/index

2 ビュー (過去 30 日間)
Janett Göhring
Janett Göhring 2012 年 8 月 23 日
Hello,
i have a cell array of 458x3 cells. The first two array columns are numeric, while the last one consists of 4 different strings ('fs','pre','sv','to'). I need to extract the data based on the strings. So, I want to create different groups out of it and make a boxplot for comparison later.
My try was:
data_fs=data{data{:,3} == 'fs',1}
(I access all rows in column 1 and meanwhile I compare the 3rd array column with 'fs')
Well, that obviously doesn't work. Any help is appreciated!
Sorry, for the trivial question. Quite new to MatLab.
thanks!
edit: It is working with boxplot(data,group). But I am still interested in how I can extract the appropriate data sets.

採用された回答

Kye Taylor
Kye Taylor 2012 年 8 月 23 日
編集済み: Kye Taylor 2012 年 8 月 23 日
You're pretty close in your attempt; it's tricky with the cell indexing and without using strcmp. How about trying this
% array of all strings used as grouping classes
groupLabels = {'fs','pre','sv','to'};
% anonymous function for id'ing rows
f = @(s)data(strcmp(s,data(:,3)),:);
% get the groups
groups = cellfun(f,groupLabels,'UniformOutput',false);

その他の回答 (2 件)

Babak
Babak 2012 年 8 月 23 日
編集済み: Babak 2012 年 8 月 23 日
You need to use strcmp() to compare strings:
for j=1:size(data,2)
if strcmp(data{j,3},'fs')
% do some stuff here for this case
else if strcmp(data{j,3},'pre')
% do some other stuff here for this case
end
end
end

Matt Fig
Matt Fig 2012 年 8 月 23 日
編集済み: Matt Fig 2012 年 8 月 23 日
I assume you mean that the each row in the third column contains one of 'fs','pre','sv',or 'to', not all 4! This snippet finds those rows that have 'fs' in the third column and returns the corresponding first columns of the cell array into a new cell array. If you want all the columns of the matching rows, change the 1 at the end to a colon.
data_fs = data(strcmp(data(:,3),'fs'),1)
  1 件のコメント
Janett Göhring
Janett Göhring 2012 年 8 月 23 日
thanks! that also helped a lot! strcmp it is ^^

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by