function to read a cell contents
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Oleg Komarov
am 7 Apr. 2013
What is the purpose? Splitting arrays is not usually recommended if you need to scale up.
Akzeptierte Antwort
Oleg Komarov
am 7 Apr. 2013
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Types finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!