我正在分析行为实验的数据。我正在将一个 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
如果我与data
交换data(i)
,我会收到错误消息。
我该怎么做?
非常感谢帮助。
答案 0 :(得分: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