Matlab 将文本文件中的数据读取到单元格数组中

Matlab 将文本文件中的数据读取到单元格数组中,matlab,Matlab,我有多个包含此格式数据的文本文件 File1.txt subID imageCondition trial textItem imageFile response RT Participant003 images 7 Is there a refrigerator? 07_targetPresent-refrigerator.jpg z 1.436971 Participant003 images 6 Is there an oven

我有多个包含此格式数据的文本文件

File1.txt

subID    imageCondition  trial   textItem    imageFile   response    RT
Participant003   images  7   Is there a refrigerator?    07_targetPresent-refrigerator.jpg   z   1.436971
Participant003   images  6   Is there an oven mitt?  06_targetPresent-ovenmitt.jpg   z   0.519301
Participant003   images  1   Is there a toaster?     01_targetAbsent-toaster.jpg     m   1.110664
Participant003   images  3   Is there a wine bottle?     03_targetAbsent-winebottle.jpg  m   1.278945
Participant003   images  2   Is there a kettle?  02_targetAbsent-kettle.jpg  z   2.672123
Participant003   images  5   Is there a blender?     05_targetPresent-blender.jpg    m   2.633802
Participant003   images  8   Is there a bucket?  08_targetPresent-bucket.jpg     m   2.596154
Participant003   images  4   Is there a surf board?  04_targetAbsent-surfboard.jpg   m   1.072850
File2.txt

subID    imageCondition  trial   textItem    imageFile   response    RT
Participant005   images  1   Is there a toaster?     01_targetAbsent-toaster.jpg         0.000000
Participant005   images  2   Is there a kettle?  02_targetAbsent-kettle.jpg  m   8.213927
Participant005   images  6   Is there an oven mitt?  06_targetPresent-ovenmitt.jpg   z   3.569293
Participant005   images  4   Is there a surf board?  04_targetAbsent-surfboard.jpg       0.000000
Participant005   images  3   Is there a wine bottle?     03_targetAbsent-winebottle.jpg  m   8.538699
Participant005   images  7   Is there a refrigerator?    07_targetPresent-refrigerator.jpg   z   0.857319
Participant005   images  5   Is there a blender?     05_targetPresent-blender.jpg        0.000000
Participant005   images  8   Is there a bucket?  08_targetPresent-bucket.jpg     z   1.967220
我希望能够将这些数据读入单元格数组,这样我就可以单独访问其中存在的值

我有下面的代码,我用它来读取数据,但它没有帮助,因为我无法以某种方式存储数据,以便我可以访问单个值。例如,我想要“试用”或“响应”列中的所有值

function content = load_data(fileName)
fid = fopen(fileName,'r')
if fid > 0
   line_no =1;
   oneline{line_no} = fgetl(fid);
   while ischar(oneline{line_no})
      line_no = line_no +1;
      oneline{line_no} = fgetl(fid);
   endwhile
   fclose(fid)
   content = oneline;
endif
endfunction


for i= 1:size(txtFiles,2)
   data{i} = load_data(txtFiles{1,i});
end

for i=1:1:length(data)
   dataMat = cell2mat(data(i));
   for j=1:1:length(dataMat)
      line = dataMat{1,j};
      % Here I'm only able to fetch lines of data as strings that are separated by more than one space characters, making it more difficult access the required data 
   endfor            
endfor
我要寻找的是一种将文本文件中的数据读取到单元格数组或矩阵中的方法,这样我就可以轻松访问所需的值,但我仅限于使用从文本文件导入数据的传统方法。或者,如果我能在解析数据时得到帮助,我可以访问所需的内容

注意:有多个这样的文本文件。另外,如果您能够展示如何访问各个列中的值,例如“response”列,这将是一个很大的帮助

function content = load_data(fileName)
fid = fopen(fileName,'r')
if fid > 0
   line_no =1;
   oneline{line_no} = fgetl(fid);
   while ischar(oneline{line_no})
      line_no = line_no +1;
      oneline{line_no} = fgetl(fid);
   endwhile
   fclose(fid)
   content = oneline;
endif
endfunction


for i= 1:size(txtFiles,2)
   data{i} = load_data(txtFiles{1,i});
end

for i=1:1:length(data)
   dataMat = cell2mat(data(i));
   for j=1:1:length(dataMat)
      line = dataMat{1,j};
      % Here I'm only able to fetch lines of data as strings that are separated by more than one space characters, making it more difficult access the required data 
   endfor            
endfor

使用strsplit之类的工具可以很容易地根据空间分割数据;除非您的textItem字段中有空格。所以我建议使用正则表达式。当您一次查找多个单独的部分时,使用是一种方便的方法来组织结果。我意识到,如果你不熟悉正则表达式,这是一件很难理解的事情。查看有关测试正则表达式的信息和非常有用的在线工具。请参见关于regex101的。这就是说,我的答案适用于您的数据:

text = fileread(filename);
data = regexp(data,'^(?<subID>\w+)\s+(?<imageCondition>\w+)\s+(?<trial>\d+)\s+(?<textItem>.*?\?)\s+(?<imageFile>[-\.\w]+)\s+(?<response>\w)\s+(?<RT>[\d\.]+)','names','lineanchors')
结果如下:

      subID           imageCondition    trial              textItem                            imageFile                  response         RT     
__________________    ______________    _____    ____________________________    _____________________________________    ________    ____________

{'Participant003'}      {'images'}      {'7'}    {'Is there a refrigerator?'}    {'07_targetPresent-refrigerator.jpg'}     {'z'}      {'1.436971'}
{'Participant003'}      {'images'}      {'6'}    {'Is there an oven mitt?'  }    {'06_targetPresent-ovenmitt.jpg'    }     {'z'}      {'0.519301'}
{'Participant003'}      {'images'}      {'1'}    {'Is there a toaster?'     }    {'01_targetAbsent-toaster.jpg'      }     {'m'}      {'1.110664'}
{'Participant003'}      {'images'}      {'3'}    {'Is there a wine bottle?' }    {'03_targetAbsent-winebottle.jpg'   }     {'m'}      {'1.278945'}
{'Participant003'}      {'images'}      {'2'}    {'Is there a kettle?'      }    {'02_targetAbsent-kettle.jpg'       }     {'z'}      {'2.672123'}
{'Participant003'}      {'images'}      {'5'}    {'Is there a blender?'     }    {'05_targetPresent-blender.jpg'     }     {'m'}      {'2.633802'}
{'Participant003'}      {'images'}      {'8'}    {'Is there a bucket?'      }    {'08_targetPresent-bucket.jpg'      }     {'m'}      {'2.596154'}
{'Participant003'}      {'images'}      {'4'}    {'Is there a surf board?'  }    {'04_targetAbsent-surfboard.jpg'    }     {'m'}      {'1.072850'}
如果要将数字字段转换为数字:

dataTable.trial = str2double(dataTable.trial);
dataTable.RT = str2double(dataTable.RT);
然后给出:

      subID           imageCondition    trial              textItem                            imageFile                  response      RT  
__________________    ______________    _____    ____________________________    _____________________________________    ________    ______

{'Participant003'}      {'images'}        7      {'Is there a refrigerator?'}    {'07_targetPresent-refrigerator.jpg'}     {'z'}       1.437
{'Participant003'}      {'images'}        6      {'Is there an oven mitt?'  }    {'06_targetPresent-ovenmitt.jpg'    }     {'z'}      0.5193
{'Participant003'}      {'images'}        1      {'Is there a toaster?'     }    {'01_targetAbsent-toaster.jpg'      }     {'m'}      1.1107
{'Participant003'}      {'images'}        3      {'Is there a wine bottle?' }    {'03_targetAbsent-winebottle.jpg'   }     {'m'}      1.2789
{'Participant003'}      {'images'}        2      {'Is there a kettle?'      }    {'02_targetAbsent-kettle.jpg'       }     {'z'}      2.6721
{'Participant003'}      {'images'}        5      {'Is there a blender?'     }    {'05_targetPresent-blender.jpg'     }     {'m'}      2.6338
{'Participant003'}      {'images'}        8      {'Is there a bucket?'      }    {'08_targetPresent-bucket.jpg'      }     {'m'}      2.5962
{'Participant003'}      {'images'}        4      {'Is there a surf board?'  }    {'04_targetAbsent-surfboard.jpg'    }     {'m'}      1.0729
您还询问了如何访问它。从表中获取第三个响应:

dataTable.response{3}
或从结构上:

data(3).response

您使用的是什么版本的Matlab?听起来新的可读表函数可能正合你的口味。嗨@AndrewJanke,我正在使用Matlab online,所以它必须是最新版本,但我想我可能会限制不使用新的可读表函数,因为这个问题来自作业,而可读表还没有教给我们。你能使用uimport函数吗?只需将文件拖放到命令窗口,但我不知道这是否适用于MATLAB online。这将打开一个GUI,帮助您配置导入,然后您可以使用所有设置让MATLAB生成导入函数。shi@max,我不能使用uimport函数。是否有一种方法可以使用用于从文本文件导入数据的代码来完成我想要的操作?我知道解析数据可能需要很多努力,但我认为我缺少选择:不幸的是,我的手被迫使用传统的方法从文本文件导入数据。Mike,你的解决方案非常有效,但有些文件有点问题。在少数文本文件中,响应列的值在某些行中为空,这会弄乱表中的数据。很抱歉,我没有发布带有这种例外情况的文件。有办法处理吗?我刚刚在我的问题中添加了一个该文件的示例作为编辑。你可以在File2.txt下找到它,我让它工作了。我在你提议的regexp中做了一点小改动就解决了这个问题\非常感谢你的回答。