【问题标题】:Matlab transformation from cell array to matrix arrayMatlab从元胞数组到矩阵数组的转换
【发布时间】:2021-07-08 05:17:49
【问题描述】:

我构建了一个脚本(来自我工作组的不同脚本)来读取文件夹中的数据。问题是最后我得到了一个 1x49 单元格的数据数组。我需要矩阵数组中的数据用于 matlab 的瀑布图。

我需要的最终矩阵在第一列中包含变量“wave”,从第 2 列到 i(我的数据数组中的单元数)是我的数据数组中的数据。

我不知道如何进入单个数组字段。

这是我为获取数组中的数据而写的:

clc;clear;      
selpath = uigetdir; 
filelist = dir(selpath);  
filelist = {filelist.name};
filter = cellfun(@(u) contains(u, '.txt'),filelist);    

data = []; 
for i = 1:numel(filelist)   
    filename = filelist{i};
    filename = [selpath '/' filename];
    delimiter = '\t';

    formatSpec = '%q%q%q%q%q%q%q%[^\n\r]';
    fileID = fopen(filename,'r');
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string',  'ReturnOnError', false);
    fclose(fileID);
    tmp_data =(dataArray{1});
    data{i} = tmp_data; 
 end

有没有什么巧妙的方法进入循环?还是我需要一个新的循环来转换它?

我试过了: data = cell2mat(data)

但这不会转换我的数据。

编辑:这是我的一个文件的外观:

Data from cystein_21101181_01.txt Node

Date: Thu Apr 08 12:37:33 CEST 2021
User: admin
Spectrometer: 211-0118
Scans to average: 16
cleanPeaks enabled: false
laserRefSpectrum enabled: true
Raster (0) enable: true
Integration Time (sec): 1.000000E0
XAxis mode: Raman Shifts
Number of Pixels in Spectrum: 1152
>>>>>Begin Spectral Data<<<<<
201.679 783.5
203.772 813.94
205.865 825.62
207.956 843
210.046 860.44
212.135 882.44
214.223 900.25
216.311 912.31

我只是将它们作为.txt 文件保存在一个文件夹中。选择脚本中的文件夹,然后脚本应导入所有光谱并将它们放在一个矩阵中。

【问题讨论】:

  • 所有data{i} 的尺寸都一样吗?为什么cell2mat 不起作用?为什么你首先创建单元格而不是矩阵?如果没有最小的可重现示例,我们就无法为您提供帮助。
  • 试试a=[ dataArray{1} ] 看看你是否得到了你想要的数组。如果可能,[ ] 括号会将元胞数组阻尼为数组。如果你想要特定的单元格,你可以[somedata{2:5}] 等......
  • @MatteoV 是的,data{i} 具有所有相同的尺寸。但是当我尝试它时,我得到了这个错误:CELL2MAT 不支持包含元胞​​数组或对象的元胞数组。这是我的代码的开始。你需要我用脚本导入的文件吗?我将编辑帖子并添加我的一个文件的内容。所有文件看起来都是这样,只是光谱仪的数据不同。
  • @bla 如果我尝试a = [dataArray{1}]我得到我的数据数组的第一列。这行得通。所以我发现我犯了一个错误。所以我总是导入文件的第一列,但我还需要第二行。也许 CELL2MAT 不起作用,因为文件开头有文本?

标签: arrays matlab loops for-loop


【解决方案1】:

鉴于您所有的文件都具有相同的结构,我认为使用readtable() 会更好。

% The start is the same as before
clc;clear;      
selpath = uigetdir; 
filelist = dir(selpath);
filelist = {filelist.name};
filter = cellfun(@(u) contains(u, '.txt'),filelist);    

data = []; 

% Initialize the settings to read your files
opts = delimitedTextImportOptions("NumVariables", 2);
opts.DataLines = [14, Inf]; % Skip the first 13 header lines
opts.Delimiter = " "; % data columns are delimited by spaces
opts.VariableTypes = ["double", "double"]; % specify the variable type for your each columns
opts.ExtraColumnsRule = "ignore"; % ignore any other column
opts.EmptyLineRule = "read"; % read lines even if they're empty
opts.ConsecutiveDelimitersRule = "join"; % treat multiple spaces as only one delimiter
opts.LeadingDelimitersRule = "ignore"; % ignore leading spaces

% Now loop through your files
for i = 1:numel(filelist)
    filename = filelist{i};
    filename = [selpath '/' filename];

    % Read the file and append the data to your array
    data = [data; table2array(readtable(filename, opts))];
    % or maybe? unsure what your wanted:
    % data{i} = table2array(readtable(filename, opts));
end

【讨论】:

  • 感谢您的代码。我试过了,但在我的最终矩阵中我只得到了 NaN。
  • 这段代码在你提供的小例子中对我有用。如果你只得到 NaN,我怀疑这可能是因为你的实际文件的结构可能会有所不同,在这种情况下,应该修改一些设置。您可以尝试将代码应用于小示例文件并检查它是否适用于该文件吗?