在matlab中绘制一些数据,如对

时间:2017-04-07 11:51:45

标签: matlab plot

我想绘制一些数据,但我不能。

假设我们在2列中有820行,表示x和y坐标。

我的代码如下:

load('MyMatFileName.mat');
[m , n]=size(inputs);
s = zeros(m,2);
for m=1:820        
    if inputs(m,end-1)<2 &   inputs(m,end)<2
        x = inputs(m,end-1)
        y = inputs(m,end)
        plot(x,y,'r','LineWidth',1.5)
        hold on
    end
end

1 个答案:

答案 0 :(得分:0)

我已经编辑了您的代码并添加了注释来解释您可能所做的更改,但请参阅下文我还重新编写了代码,以便更像应该< / em>完成:

load('MyMatFileName.mat');  % It's assumed "inputs" is in here
% You don't use the second size output, so use a tilde ~
[m, ~] = size(inputs);
%s = zeros(m,2); % You never use s...

% Use a different variable for the loop, as m is already the size variable
% I've assumed you wanted ii=1:m rather than m=1:820
figure
hold on   % Use hold on and hold off around all of your plotting
for ii=1:m
    if inputs(m,end-1)<2 && inputs(m,end)<2 % Use double ampersand for individual comparison
        x = inputs(m,end-1)
        y = inputs(m,end)
        % Include the dot . to specify you want a point, not a line!
        plot(x, y, 'r.','LineWidth',1.5)
    end
end
hold off

在Matlab中完成整个操作的更好方法是将代码矢量化:

load('MyMatFileName.mat');
[m, ~] = size(inputs);
x = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end-1);
y = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end);
plot(x, y, 'r.', 'linewidth', 1.5);

请注意,这将绘制点,如果要绘制线,请使用

plot(x, y, 'r', 'linewidth', 1.5); % or plot(x, y, 'r-', 'linewidth', 1.5);