Matlab:循环文件,在一个图中绘制数据&将文件名添加为图例

时间:2016-06-28 16:17:27

标签: matlab loops plot matlab-figure

我有一个包含多个文件的文件夹,每个文件包含数百个数据对(温度T上的电阻R)。这些文件不包含相同数量的数据点...... 我希望Matlab读取文件,循环它们并绘制每个文件的R(T),但所有这些都在一个图中。此外,我希望文件名作为不同图形的图例(例如,形式文件'Example1.dat'的图形应在图例中表示为'Example1.dat')。 我现在正在做的是以下内容:

files=dir('*.dat') % Get all input files
hold on % multiple plots in one figure
for file=files' % loop over files
    [T, R] = textread(file.name,'%f %f') %get data points
    xlim([8.5 10]) % set limits
    ylim([-0.5 2.5]) % set limits
    plot(T,R) % plot
end
legend(files.name) % add legend

我得到的看起来不正确,因为每次尝试时,相同的图形在图例中都会有不同的名称。我该如何解决?

1 个答案:

答案 0 :(得分:0)

试试这个

files=dir('*.dat') % Get all input files
hold on % multiple plots in one figure
for filenumber=1:length(files) % loop over files
    [T, R] = textread(files(filenumber).name,'%f %f') %get data points
    plot(T,R) % plot
end
%limits only need to be fixed at the end
xlim([8.5 10]) % set limits
ylim([-0.5 2.5]) % set limits
legend(files.name) % add legend
相关问题