0

我正在分析来自行为实验的数据。我正在将我的数据从一个excel 文件读入 matlab。

% load data
[num,text,raw] = xlsread('stop');
data=num(:,10); % column 10 in original excel file

调用的变量data是一个包含所有受试者结果的单列向量 (25000x1)。

data(1:227) is from subject 1. 
data(228:420) is from subject 2, and so on. 

在下面查看我的部分数据:

Subject ID    Data
10011         384
10011         290
10011         508
10033         322
10033         361
10033         522

我需要得到一个名为"data". 这些变量 ( "data") 应包含每个主题的所有数据。

到目前为止,我已经为每个主题手动创建了一个变量 - 这是不切实际的(100 个主题):

x1=find(num(:,1)==10011);
px1=data(x1,:); and so on up to 100th subject. 

如果这个过程可以像我在下面尝试过的那样自动化,那就更好了:

subjects=[10011; 10033...];
for i=1:length(subjects)
 data=num((find(num(:,1)==subjects(i))),10);
end

如果我与我交换datadata(i)我会收到一条错误消息。我该怎么做?

帮助将不胜感激。

4

1 回答 1

0

你可以这样做:

subjects=unique(num(:,1)); %note that 'subjects' will be sorted.
data=[];
for i=1:length(subjects)
    temp1=num((find(num(:,1)==subjects(i))),10);
    %now two options, either save the data for each subject in a different cell, as follows:
    data{i}=temp1; %if you do this, you can replace temp with data{i}. Also remove the line: data=[];
    %or save it in a matrix but then you cannot differentiate between data of different subjects. For that, you have to write another code.
    data=[data;temp]; %this is where you need temp
end
于 2013-03-15T19:35:23.817 回答