简体   繁体   English

Matlab使用循环创建单元格数组

[英]Matlab create cell array using a loop

As part of a university project, I have to do the following: Use a database that contains 14 different folders (14 different subjects) and in every folder we have 5 trials of 12 different activities stored in .mat files. 作为大学项目的一部分,我必须执行以下操作:使用包含14个不同文件夹(14个不同主题)的数据库,并且在每个文件夹中,我们对.mat文件中存储的12种不同活动进行了5次试用。 The trials files contain 6 columns (of which I want only 3) and different size of rows. 试用文件包含6列(我只需要3列)和不同大小的行。 Now I want to concatenate the 5 trials in one .mat folder for every subject, and store all the database to the following format : subject x activity x trial . 现在,我想将5个试验连接到每个主题的.mat文件夹中,并将所有数据库存储为以下格式:subject x activity x trial。

I have written the following code: 我写了以下代码:

InPath_data = 'C:\Users\olga\Desktop\USC-HAD\';

listname = [InPath_data, 'Subjects.txt'];
folder_subject = textread(listname,'%s','delimiter','\n','whitespace','');
listname = [InPath_data,'ActivityTrial.txt'];
files_act_tr = textread(listname,'%s','delimiter','\n','whitespace','');
fname = [InPath_data,folder_subject{1},'\',files_act_tr{1},'.mat'];
tmp1= [];
k=1;
for i=1:length(folder_subject)
    for j=1:length(files_act_tr)
        fname = [InPath_data,folder_subject{i},'\',files_act_tr{j},'.mat'];
        tmp= load(fname);
        q=tmp.sensor_readings( : ,(1:3));
        tmp1= cat(1,tmp1,q);
        clear q;
        if mod(j,5)==0
         data(j-4*k,:,:)=tmp1;
         k=k+1;
         tmp1=[];
        end
    end
end

Now when clearing tmp1 i cannot use the loop. 现在,当清除tmp1时,我无法使用循环。 I suspect that using cell arrays is the key to do what I want, but I have never used them before, so... Any help would be appreciated! 我怀疑使用单元阵列是完成我想要的工作的关键,但是我以前从未使用过它们,所以...任何帮助将不胜感激! :) :)

clear tmp1; deletes the variable, in the next iteration the variable tmp1 is unknown like it was never used before. 删除变量,在下一次迭代中,变量tmp1是未知的,因为以前从未使用过。 Using tmp1=[] instead empties the variable. 而是使用tmp1=[]清空变量。 This should fix your code. 这应该修复您的代码。

Edit: I managed to do it, by setting a stable array size for trials to 10000x3.Otherwise, the size changes over loops and the 4 D matrix cannot be formed properly. 编辑:我设法通过将试验的稳定数组大小设置为10000x3来做到这一点,否则,大小会在循环中变化并且4D矩阵无法正确形成。 Using @Daniel R solution tmp1=[] and some other alterations, the code goes like this: 使用@Daniel R解决方案tmp1 = []和其他一些更改,代码如下所示:

InPath_data = 'C:\Users\olga\Desktop\USC-HAD\';

listname = [InPath_data, 'Subjects.txt'];
folder_subject = textread(listname,'%s','delimiter','\n','whitespace','');
listname = [InPath_data,'ActivityTrial.txt'];
files_act_tr = textread(listname,'%s','delimiter','\n','whitespace','');
fname = [InPath_data,folder_subject{1},'\',files_act_tr{1},'.mat'];
tmp1= [];
k=1;

for i=1:length(folder_subject)
    for j=1:length(files_act_tr)
        fname = [InPath_data,folder_subject{i},'\',files_act_tr{j},'.mat'];
        tmp= load(fname);
        q=tmp.sensor_readings( : ,(1:3));
        tmp1= cat(1,tmp1,q);
        clear q;

        if mod(j,5)==0

         data(i,j-4*k,:,:)= tmp1((1:10000), :);
         k=k+1;
         tmp1=[];

        end

    end
   k=1;
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM