目前,我有一个txt文件,其数据如下所示:
A11
Temperature=20 Weight=120 Speed=65
B13
Temperature=21 Weight=121 Speed=63
F24
Temperature=18 Weight=117 Speed=78
D43
Temperature=16 Weight=151 Speed=42
C32
Temperature=15 Weight=101 Speed=51
我想将值读入单元格数组并将其转换为矩阵。 下面是我的代码:
% At first I read the data into a 1 column array
fid=fopen('file.txt');
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
% Then I check the size of the cell array
CellSize = size(tlines);
DataSize = CellSize(1,1);
% At last I setup a loop and literately read and input the values
Data = cell(0,3);
for i = 1:DataSize
Data{end+1,3} = textscan(tlines{i,1},'Temperature=%f Weight=%f Speed=%f');
end
但是,我有10x3的空单元格数组。
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
[] [] *1x3cell*
我知道问题出在将textscan值输入到单元格数组中。您能帮我解决问题吗?另外,如果数据不包含特定格式,我该如何扔空值。
答案 0 :(得分:0)
您犯的唯一错误是使用Data
索引{}
的内容,而不是使用()
索引单元格,请参见help。
我修改了脚本的最后一部分:
% At last I setup a loop and literately read and input the values
Data = cell(DataSize, 3);
for i = 1:DataSize
Data(i, :) = textscan(tlines{i}, 'Temperature=%f Weight=%f Speed=%f')
end
给出以下输出:
Data =
{
[1,1] = [](0x1)
[2,1] = 20
[3,1] = [](0x1)
[4,1] = 21
[5,1] = [](0x1)
[6,1] = 18
[7,1] = [](0x1)
[8,1] = 16
[9,1] = [](0x1)
[10,1] = 15
[1,2] = [](0x1)
[2,2] = 120
[3,2] = [](0x1)
[4,2] = 121
[5,2] = [](0x1)
[6,2] = 117
[7,2] = [](0x1)
[8,2] = 151
[9,2] = [](0x1)
[10,2] = 101
[1,3] = [](0x1)
[2,3] = 65
[3,3] = [](0x1)
[4,3] = 63
[5,3] = [](0x1)
[6,3] = 78
[7,3] = [](0x1)
[8,3] = 42
[9,3] = [](0x1)
[10,3] = 51
}
随后您可以执行以下操作:
% Clean up.
for i = DataSize:-1:1
if (isempty([Data{i, :}]))
Data(i, :) = [];
end
end
这样您的输出看起来像这样:
Data =
{
[1,1] = 20
[2,1] = 21
[3,1] = 18
[4,1] = 16
[5,1] = 15
[1,2] = 120
[2,2] = 121
[3,2] = 117
[4,2] = 151
[5,2] = 101
[1,3] = 65
[2,3] = 63
[3,3] = 78
[4,3] = 42
[5,3] = 51
}
如果事先知道大小,请不要执行Data{end+1,3}
之类的操作。我也对此做了相应的修改。
答案 1 :(得分:0)
如果要保留标题/名称,这并不是很优雅,但是可以使用:
error : undefined method `put' for <AWS::S3::S3Object:bucketname/folder/subfolder/>:AWS::S3::S3Object