Hi Samuele,
As I understand while executing your code you are facing errors.
According to error shown, the inputs to plot() function must be of same length but in the call to plot the X,Y does not seem to be of same length.
This can be verified by the documentation you provided
% x - vector of x values [optional, can be left empty]
% y - vector of y values or a matrix of n observations by m cases
% where m has length(x);
Before plotting you can verify whether X is empty or not and if empty you can initialize it as per your need.
Follow the link below for further understanding of plot()