Extracting data from a cell array problem

5 ビュー (過去 30 日間)
Amy Hassett
Amy Hassett 2020 年 4 月 14 日
コメント済み: Stephen23 2020 年 4 月 14 日
Hi folks,
I have extracted data from an SQLite file into MATLAB, and have it saved as a (1000,4) cell array, wherein the contents of the 4th column are numbers (1, 2 or 3). I am trying to extract rows of data from this cell array on the basis of the contents of the 4th column (see code below), but MATLAB tells me that I cannot perform this particular operation on the contents of a cell.
results_solo = fetch(conn, sqlquery_soloEvents);
AnimalData = 'SELECT ID, RFID, GENOTYPE, NAME FROM ANIMAL';
results_animalData = fetch(conn , AnimalData);
% Create a structure containing our data
for k = 1:size(results_animalData)
data(k).Mice = results_animalData(k,4);
data(k).Genotype = results_animalData(k, 3);
data(k).RFID = results_animalData(k,2);
data(k).ID = cell2mat(results_animalData(k,1));
data(k).Behaviours = results_solo(results_solo(:,4) == k, :);
end
Therefore, I rewrote the code using strcmpi to the following:
for k = 1:size(results_animalData)
data(k).Mice = results_animalData(k,4);
data(k).Genotype = results_animalData(k, 3);
data(k).RFID = results_animalData(k,2);
data(k).ID = cell2mat(results_animalData(k,1));
data(k).Behaviours = results_solo(strcmpi(results_solo(:,4), k), :);
end
But this is not working either. So then I tried to directly access the contents of my cell as follows:
for k = 1:size(results_animalData)
data(k).Mice = results_animalData(k,4);
data(k).Genotype = results_animalData(k, 3);
data(k).RFID = results_animalData(k,2);
data(k).ID = cell2mat(results_animalData(k,1));
data(k).Behaviours = results_solo(results_solo{:,4} == k, :);
end
But that is not working. (it gives an error saying that in the final line I have too many input arguments).
Can someone point me towards what I should do next?
  2 件のコメント
Tommy
Tommy 2020 年 4 月 14 日
Try
data(k).Behaviours = results_solo(cellfun(@(c) c==k, results_solo(:,4)), :);
Amy Hassett
Amy Hassett 2020 年 4 月 14 日
This also worked! Thank you!

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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 14 日
編集済み: Ameer Hamza 2020 年 4 月 14 日
Try this
data(k).Behaviours = results_solo([results_solo{:,4}] == k, :);
%^ add square brackets
  3 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 4 月 14 日
Glad to be of help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by