Matlab----绘图以及文件储存

目录

二维曲线

基础函数:plot/fplot

绘制图形的辅助操作

文件存储 


二维曲线

基础函数:plot/fplot

(1)plot函数的基本用法:plot(x,y)其中x和y分别用于储存x坐标和y坐标数据

(2)最简单plot函数调用格式 :plot(x) 

          >>x=[1.5,2,1,1.5];

          >>plot(x)

(3)当plot函数的参数x是负数向量时,则分别以该向量元素实部和虚部为横纵坐标绘制一条曲线

 (4)plot(x,y)函数参数的变化形式

当x是向量,y为矩阵

  • 如果矩阵y的列数等于x的长度,则以向量x为横坐标,以y的每个行向量为纵坐标绘制曲线曲线的条数等于y的行数
  • 如果矩阵y的行数等于x的长度,则以向量x为横坐标,以y的每个列向量为纵坐标绘制曲线曲线的条数等于y的列数

(5)含多个输入参数的plot函数: plot(x1,y1,x2,y2,....,xn,yn)  其中,每一向量对勾成一组数据的横纵坐标绘制一条曲线

绘制图形的辅助操作

%create coordinate variables and plot
x = 1:6;
y = [1 5 3 9 11 8];
plot(x,y,'k*-.');%%color+marker+line
hold %%反转画布状态画布
ishold %%判断是否擦画布,输出逻辑矩阵

%ishold = false
hold on
%ishold = true
hold off
%ishold = false


%%change the axes and label
%%axis([0 8 0 15]);限定绘图板大小
xlabel('Time1');
ylabel('Temperature1');

%%Put a title on the plot
title('Time and Temperature1');

figure();
plot(x,x+1,'x');

%%change the axes and label
%%axis([0 8 0 15]);限定绘图板大小
xlabel('Time2');
ylabel('Temperature2');

%%Put a title on the plot
title('Time and Temperature2');

figure(1); %figure 1 active,figure 2 inactive

plot(x,x+1,'o-');%%切记不要点击绘图板窗口,可能改变选择目标

close(1)  %%完全将figure(1)删除不留信息

 具体参照help plot

ishold=false;%%起初为擦画布----0
plot(x,y);
%%plot #1

hold;%%反转是否擦画布----1
plot(x,x+1,'x-');
%%plot #1+plot#2


hold;-----0
plot(x,x+2,'o-')
%%plot #3

%%最后只显示o-点线
%create coordinate variables and plot

close all;

figure();

x = 1:6;
y = [1 5 3 9 11 8];

subplot(2,1,1);%%分块矩阵第一块
plot(x,y);hold on;

subplot(2,1,2);%%第二块
plot(x,y);hold on;

cla %%清空选中的元素,默认为上方刚执行命令
clf;
%%change the axes and label
xlabel('Time1');
ylabel('Temperature1');

%%Put a title on the plot
title('Time and Temperature1');

figure
plot(rand(3,1),'x--')
hold on
plot(rand(3,1),'o:')
plot(rand(3,1),'s-')
grid %%打开网格
legend('Line 1','Line 2','Line 3')%%做图例
bar(1:10,randi([1,10],[1,10]))  %%做条状图
hold on 
plot(1:0.1:10,sin(1:0.1:10),1:0.1:10,cos(1:0.1:10)) %%绘制两条曲线
xlabel('x');
ylabel('y')
title('figure 1')
grid on
legend('bar','plot #1','plot #2')

文件存储 

1.save+文件名.dat/,txt+数据名+数据类型 

clear
% read from file
% written to file
% append to file
mymat = rand(2,3);
save testfile.dat mymat -ascii%%生成文件
type testfile.dat %%查看文件内容
mymat2 = rand(4,3);
save testfile.dat mydat2 -ascii
type testfile.dat  %%显示第二次写入的变量,将第一次覆盖
myhat3 =rand(4,3);
%%append在上方矩阵文件中添加myhat3,不要求矩阵列相等
save testfile.dat mydat2 -ascii - append 

例题:

%Prompt the user for the number of rows and columns of a matrix,create a matrix
%with that many rows and columns of input, This matrix has element as random
%integer from 1to 100.Write this matrix to a file named 'randMat.dat'
r = input('Enter the number of rows: ');
c = input('Enter the number of columns: ');
matrix = randi([1,100],[r,c]);
save randMat.dat matrix -ascii 

猜你喜欢

转载自blog.csdn.net/weixin_62436283/article/details/126535954
今日推荐