interactive basic matlab with plot

I have been programming a lot in matlab recently. However, I was not told about some of the cool tricks in class.

For a beginner, getting interactive user input and how to generate plot and save it would be interesting.

so here is an example. just enter a number ‘b’ and we will generate a vector y, that is b random numbers between -10 and 10 and plot it by using x = 1,2,3,…,b and y = y1, y2, y3,…,yb. and plot it and save the picture.

 

 

%% this scripts shows how to get user input

clear all
% to clear everything in the work space
% to avoid that a variable already exist in our workspace

%enter b
b = input(‘enter b: ‘);
%generate b random integer between -10 and 10.
y = randi([-10,10],1,b);

x = 1:1:b;

figure2 = figure(‘position’, [0, 0, 700, 500]);
plot(x,y,’b*’,’MarkerSize’,10)

xlabel(‘x’,’FontSize’,14)
ylabel(‘y’,’FontSize’,14)
title(‘test plot’, ‘FontSize’, 16)

saveas(figure2,’test1.png’);