我有一个包含15个数据块的.csv文件(每个块由两行零分隔)。每个块是16x16。
我想将它导入MATLAB中的单元格,这样我就可以轻松访问每个单独的块进行分析。这是我到目前为止所尝试的内容,但是fgetl
和fgets
删除了矩阵格式,也没有给出我想要的单个单元格。
fileID_cos = fopen('variable_brillouin_cos.csv');
Block = 1;
while (~feof(fileID_cos))
fprintf('Block: %s\n', num2str(Block)) % Print block number to the screen
InputText = fgetl(fileID_cos);
comp = strcmp(InputText,'0');
if comp == 0
Data{Block,1} = InputText;
Block = Block + 1; % Increment block index
else
% Do nothing - don't read
end
end
我也尝试过使用textscan
,但我不太清楚如何让它发挥作用。我在这里关注这个例子:http://www.mathworks.com/help/matlab/examples/reading-arbitrary-format-text-files-with-textscan.html
编辑:以下是我的.csv文件(2x3x16)中前两个数据块的摘录:
0.9992287094 0 0
-0.0014847289 0.0582334756 0
-0.0342878228 0.007099638 0.0460434373
0.005238545 -0.014102395 -0.0064428952
0.0041854498 0.0016727184 -0.0063756844
-0.0010147738 0.0025424322 0.0019445089
-0.0008249775 -0.00102181 0.0011112002
0.0002007113 -0.0003419567 -0.000332161
0.0002909916 6.47733298552847E-05 -0.000372398
-4.97697754763672E-05 2.53135160405396E-05 1.37384469745553E-05
-6.69268915155018E-05 5.17541889751699E-05 0.000115002
-2.25078759011089E-05 -9.4409769330572E-05 4.519931558177E-05
5.07028561121694E-05 -7.32595335398115E-05 -7.90541160988023E-05
2.52228828927873E-05 3.13998660159052E-05 -3.59807082625053E-05
-2.26997218734607E-05 8.41379631611071E-06 3.2902284592782E-05
-0.000019695 -4.86435490970069E-05 2.58425361765046E-05
0 0 0
0 0 0
0.9952580275 0 0
-0.0014741309 0.0577730556 0
-0.0338816221 0.0070146088 0.0454970609
0.0051560767 -0.0138844528 -0.0063416486
0.0041060663 0.0016393367 -0.0062540706
-0.0009919008 0.0024819681 0.0018997603
-0.0008019338 -0.0009956338 0.0010798323
0.0001948589 -0.0003332688 -0.0003221603
0.0002816506 6.2320254232888E-05 -0.0003605357
-4.87388321597287E-05 2.29788680538837E-05 1.42137144094536E-05
-6.35666587570066E-05 4.81815519669417E-05 0.000109376
-2.13596699829001E-05 -9.08958604638168E-05 4.25332719521993E-05
4.85046575990093E-05 -6.99485838406453E-05 -7.55865872986199E-05
2.32942086120719E-05 2.81906152684917E-05 -3.33507607030521E-05
-2.07229114591174E-05 6.79039983723664E-06 3.01930791396076E-05
-1.85259428148148E-05 -4.62398737406514E-05 2.3827396784366E-05
答案 0 :(得分:1)
正如我所看到的,你从这里尝试过这个例子:
如果您只有一行零来分隔块,这将起作用。或者您需要添加一些代码来检查相邻的零行。
对于你的情况,我试过以下:
fileID_cos = fopen('variable_brillouin_cos.csv');
%initialize the array
dataCell = {};
% initialize counters
zeroCheck = 0;
counterCol = 1;
counterRow = 1;
lineNumber = 0;
lineNumberZeroLine = 0;
while (~feof(fileID_cos))
%fprintf('Block: %s\n', num2str(counterCol )) % Print block number to the screen
InputText = fgetl(fileID_cos);
% get the data. the last number represents the number of column in your
% csv file
currentLineData = sscanf(InputText, '%f', 3);
% current line number
lineNumber = lineNumber + 1;
% check adjacent zero rows
if (sum(currentLineData) == 0) && zeroCheck == 0
zeroCheck = zeroCheck + 1;
lineNumberZeroLine = lineNumber;
elseif ((sum(currentLineData) == 0) && zeroCheck == 1)
% if adjacent
if ((lineNumber - lineNumberZeroLine) == 1)
zeroCheck = zeroCheck + 1;
lineNumberZeroLine = 0;
else
zeroCheck = 0;
lineNumberZeroLine = lineNumber;
end
end
% if two rows of zero encountered
if (zeroCheck ~= 2)
dataCell{counterRow,counterCol} = currentLineData;
counterRow = counterRow + 1;
else
% if all blocks have same number of rows, use following line
% which removes the last row of the cell array
% dataCell(counterRow -1,:) = [];
% reset & modify counters
counterCol = counterCol + 1;
zeroCheck = 0;
counterRow = 1;
end
end
fclose(fileID_cos);
结果单元阵列 dataCell 的每一列是一个块。当您使用单元格数组时,忽略最后一行(在您的示例中,忽略单元格数组中的第17行。检查代码中的注释以删除此行。)
由于计数器代码看起来有点复杂,但它有效:)