Matlab interp1 将最后一行作为 NaN
Matlab interp1 gives last row as NaN
我遇到了类似于 here 的问题。不过好像没有解决办法。
我的问题是这样的:我需要导入一些文件,比如5个,每个文件有20列,但是行数不一样。第一列是曲柄转角时间,其余为数据。
所以我的代码首先导入所有文件,找到行数最多的文件,然后创建一个包含那么多行的多维数组。时间是在引擎循环中,所以我会从导入的文件中删除超出整个引擎循环的行。这样,我总是有 X 个整个引擎循环的数据。然后我将数据插入到预先分配的数组中,为 5 个数据文件提供一个巨大的多维数组。
然而,这似乎总是导致每页每列的最后一行都被 NaN 填充。请看下面的代码。我看不出我哪里做错了。哦,顺便说一句,因为我之前被搞砸了,这不是作业。
maxlines = 0;
maxcycle = 999;
for i = 1:1
filename = sprintf('C:\Directory\%s\file.out',folder{i});
file = filelines(filename); % Import file clean
lines = size(file,1); % Find number of lines of file
if lines > maxlines
maxlines = lines; % If number of lines in this file is the most, save it
end
lastCAD = file(end,1); % Add simstart to shift the start of the cycle to 0 CAD
lastcycle = fix((lastCAD-simstart)./cycle); % Find number of whole engine cycles
if lastcycle < maxcycle
maxcycle = lastcycle; % Find lowest number of whole engine cycles amongst all designs
end
cols = size(file,2); % Find number of columns in files
end
lastcycleCAD = maxcycle.*cycle+simstart; % Define last CAD of whole cycle that can be used for analysis
% Import files
thermo = zeros(maxlines,cols,designs); % Initialize array to proper size
xq = linspace(simstart,lastcycleCAD,maxlines); % Define the CAD degrees
for i = 1:designs
filename = sprintf('C:\Directory\%s\file.out',folder{i});
file = importthermo(filename, 6, inf); % Import the file clean
[~,lastcycleindex] = min(abs(file(:,1)-lastcycleCAD)); % Find index of end of last whole cycle
file = file(1:lastcycleindex,:); % Remove all CAD after that
thermo(:,1,i) = xq;
for j = 2:17
thermo(:,j,i) = interp1(file(:,1),file(:,j),xq);
end
sprintf('file from folder %s imported OK',folder{i})
end
thermo(end,:,:) = []; % Remove NaN row
非常感谢您的帮助!
您的采样是否超出范围?如果是这样,您需要告诉 interp1
您想要外推
interp1(file(:,1),file(:,j),xq,'linear','extrap');
我遇到了类似于 here 的问题。不过好像没有解决办法。
我的问题是这样的:我需要导入一些文件,比如5个,每个文件有20列,但是行数不一样。第一列是曲柄转角时间,其余为数据。
所以我的代码首先导入所有文件,找到行数最多的文件,然后创建一个包含那么多行的多维数组。时间是在引擎循环中,所以我会从导入的文件中删除超出整个引擎循环的行。这样,我总是有 X 个整个引擎循环的数据。然后我将数据插入到预先分配的数组中,为 5 个数据文件提供一个巨大的多维数组。
然而,这似乎总是导致每页每列的最后一行都被 NaN 填充。请看下面的代码。我看不出我哪里做错了。哦,顺便说一句,因为我之前被搞砸了,这不是作业。
maxlines = 0;
maxcycle = 999;
for i = 1:1
filename = sprintf('C:\Directory\%s\file.out',folder{i});
file = filelines(filename); % Import file clean
lines = size(file,1); % Find number of lines of file
if lines > maxlines
maxlines = lines; % If number of lines in this file is the most, save it
end
lastCAD = file(end,1); % Add simstart to shift the start of the cycle to 0 CAD
lastcycle = fix((lastCAD-simstart)./cycle); % Find number of whole engine cycles
if lastcycle < maxcycle
maxcycle = lastcycle; % Find lowest number of whole engine cycles amongst all designs
end
cols = size(file,2); % Find number of columns in files
end
lastcycleCAD = maxcycle.*cycle+simstart; % Define last CAD of whole cycle that can be used for analysis
% Import files
thermo = zeros(maxlines,cols,designs); % Initialize array to proper size
xq = linspace(simstart,lastcycleCAD,maxlines); % Define the CAD degrees
for i = 1:designs
filename = sprintf('C:\Directory\%s\file.out',folder{i});
file = importthermo(filename, 6, inf); % Import the file clean
[~,lastcycleindex] = min(abs(file(:,1)-lastcycleCAD)); % Find index of end of last whole cycle
file = file(1:lastcycleindex,:); % Remove all CAD after that
thermo(:,1,i) = xq;
for j = 2:17
thermo(:,j,i) = interp1(file(:,1),file(:,j),xq);
end
sprintf('file from folder %s imported OK',folder{i})
end
thermo(end,:,:) = []; % Remove NaN row
非常感谢您的帮助!
您的采样是否超出范围?如果是这样,您需要告诉 interp1
您想要外推
interp1(file(:,1),file(:,j),xq,'linear','extrap');