【问题标题】:Read data from files using textscan & plot histogram/ecdf using plotyy使用 textscan 从文件中读取数据并使用 plotyy 绘制直方图/ecdf
【发布时间】:2012-06-22 08:14:20
【问题描述】:

我尝试将具有 20x20 数据的文本文件读入变量 C,并尝试在左侧 Y 轴上绘制直方图,在右侧 X 轴上绘制 ecdf/ksdensity。

使用 textscan 将数据读入 400x1 数组。但是,当我尝试调用 plotyy 来绘制直方图时,下面的代码返回

Error using plot
Vectors must be the same lengths. 

Error in pt (line 11)
axes = plotyy(x,C{1},x,C{1});

我猜这是由于 C{1}。但不知道如何解决它。我是matlab新手,有人能指出执行上述操作的正确方法吗?

fid = fopen('t1_error.txt','r');
C = textscan(fid, '%.3f');            
fclose(fid);

disp(C{1});

x = -1:7;    % <-- change to x = length(C{1});  then histogram is plotted.
axes = plotyy(x,C{1},x,C{1});
hold on
hist(axes(1), C{1}, x);
ylim(axes(1),'auto');
set(axes(1),'mode','auto');
hold off

【问题讨论】:

  • x 的长度为 9,C{1} 有多长?估计是200?但它必须具有相同的长度。如果您只是绘制一个向量,为什么要使用 plotyy?试试 x = 1:200。
  • 啊,直方图在我更改 x=1:400 后显示。但是,我最初的意图是在单个图中绘制两个图:X:错误率,Y1(左轴,直方图):概率,Y2(右轴,线):ecdf。在此之前,我尝试使用下面的代码绘制一个独立的直方图。 % x_step = -1:0.1:7; % 历史 (C{1}, x_step); % xlabel('相对误差'); % ylabel('概率');
  • 所以问题中的代码不是最新的?您能否更新问题,使其显示您所做的 x = 1:400 更改?

标签: matlab matlab-figure


【解决方案1】:

x 的长度不等于C{1} 的长度。试试x = 1:length(C{1})x = -1:8/length(C{1}):7

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2012-04-02
    • 2017-05-15
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多