MATLAB中的子绘图中的图形绘制不正确

MATLAB中的子绘图中的图形绘制不正确,matlab,plot,graph,matlab-figure,subplot,Matlab,Plot,Graph,Matlab Figure,Subplot,我有一个,它有3列,第一列是数据字段,或者你可以说是不同的索引。第2列是x轴数据,第3列是y轴数据。现在我有类似的数据文件用于不同的变量,比如8个文件。我想在MATLAB中将所有图形绘制在一个图形中。对于我的问题,我只显示一个子地块。此子地块数据文件应为5个索引(第1列)绘制5个“线图”。但当我把它画成子图时,它只显示一个图。下面是我的代码: % open Zdiff Odd Mode data file fid = fopen('Data_test.txt'); % Read data in

我有一个,它有3列,第一列是数据字段,或者你可以说是不同的索引。第2列是x轴数据,第3列是y轴数据。现在我有类似的数据文件用于不同的变量,比如8个文件。我想在MATLAB中将所有图形绘制在一个图形中。对于我的问题,我只显示一个子地块。此子地块数据文件应为5个索引(第1列)绘制5个“线图”。但当我把它画成子图时,它只显示一个图。下面是我的代码:

% open Zdiff Odd Mode data file
fid = fopen('Data_test.txt');
% Read data in from csv file
readData = textscan(fid,'%f %f %f','Headerlines',1,'Delimiter',',');
fclose(fid);

% Extract data from readData
index_Data = readData{1,1}(:,1);
% Identify the unique indices
uni_idx=unique(index_Data);
xData = readData{1,2}(:,1);
yData = readData{1,3}(:,1);


% Plot Data
f = figure;
%Top Title for all the subplots
p = uipanel('Parent',f,'BorderType','none'); 
p.Title = 'Electrical Characteristics'; 
p.TitlePosition = 'centertop'; 
p.FontSize = 14;
p.FontWeight = 'bold';

cla; hold on; grid on, box on;

ax1 = subplot(2,4,1,'Parent',p);
% Loop over the indices to plot the corresponding data
for i=1:length(uni_idx)
   idx=find(index_Data == uni_idx(i));
   plot(xData(idx,1),yData(idx,1))
end
绘图结果如下所示:

当我把数据绘成一个完整的图形时,这个图形是完美的。但是由于我有很多数据要在一个图中绘制为子图,所以我需要知道我的子图代码中有什么错误

这是我的代码,用于不带子图的整个数据图

在打印代码之前,与之前相同:

% Plot Data
f1 = figure(1);
cla; hold on; grid on;

% Loop over the indices to plot the corresponding data

for i=1:length(uni_idx)
   idx=find(index_Data == uni_idx(i));
   plot(xData(idx,1),yData(idx,1))
end
得出的数字如下:


子地块中的绘图代码有什么问题?有人能帮我吗?

这是您的命令序列,以及它们的作用:

f = figure;
创建一个空图形,此处尚未定义轴

cla
清除当前轴,因为没有当前轴,所以会创建一个

hold on
设置当前轴上的“保持”属性

grid on, box on
ax1 = subplot(2,4,1,'Parent',p);
设置当前轴的一些其他特性

grid on, box on
ax1 = subplot(2,4,1,'Parent',p);
创建新轴。因为它覆盖了以前创建的轴,所以这些轴将被删除

plot(xData(idx,1),yData(idx,1))
打印到当前轴(即由
子地块创建的轴)。这些轴没有“保持”特性集,因此后续的
plot
命令将覆盖此处绘制的数据

解决方案是设置由
子地块创建的轴的“保持”属性。替换:

cla; hold on; grid on, box on;
ax1 = subplot(2,4,1,'Parent',p);
与:


(请注意,
cla
是不必要的,因为您正在绘制一个新的空图形)。

您需要在每个子批次中保持不变,而不是在之前。只有这样才是轴心selected@AnderBiguri非常感谢……真是个愚蠢的错误!