function to read a cell contents

조회 수: 1 (최근 30일)
Nicolò
Nicolò 2013년 4월 7일
Hello everybody! As the example shows, i would like that the genre of the variable will be recognized and declared (with print at the screen) and then divided creating two different matrices.Does anybody know the function that i need?
data = {'tomato','VEGETABLE';'banana','FRUIT';'grapes','FRUIT';'pepper','VEGETABLE';'carrot','VEGETABLE';'pear','FRUIT'}
class1 = 'FRUIT'
class2 = 'VEGETABLE'
data1 = {'tomato','VEGETABLE';'pepper','VEGETABLE';'carrot','VEGETABLE'}
data2 = {'banana','FRUIT';'grapes','FRUIT';'pear','FRUIT'}
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2013년 4월 7일
What is the purpose? Splitting arrays is not usually recommended if you need to scale up.
Nicolò
Nicolò 2013년 4월 7일
my purpose is select from the input database only the values that i'm interesting in thanks to the "kind's" recognizement (of course the type is unknown)

댓글을 달려면 로그인하십시오.

채택된 답변

Oleg Komarov
Oleg Komarov 2013년 4월 7일
I discourage creating incrementally numbered variables in the workspace, however here's the bare-bone approach:
% Index vegetables
idx = strcmpi(data(:,2),'vegetable');
% Select vegetable entries
data(idx,1)
Now, you can automate and throw everything into a, e.g. structure:
% Throw everything into a structure
[unData, ~, idxData] = unique(data(:,2));
for n = 1:numel(unData)
s.(unData{n}) = data(idxData == n,1);
end
s.FRUIT
s.VEGETABLE

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by