chaoshu的个人博客分享 http://blog.sciencenet.cn/u/chaoshu

博文

matlab常用方法

已有 3808 次阅读 2016-9-18 21:52 |个人分类:matlab|系统分类:科研笔记

之前将数组或者矩阵保存为一个mat格式的文件,在进行load命令读取时:

s1=load('qiyipuzong.mat');

得到的s1是struct类型的数据,而我想要的是一个矩阵或者数组。

经过搜索查询,参考下面这个链接的内容

http://baike.baidu.com/link?url=k4G5EtvGr_zj_MgN2jnjxLFTM9m8hC1nYwUbCAXB3_cVwKBcpcSKMu1PuRhVlgutxhDAW9K3ehAh4M1ahRM4cq

,将命令:

s1=load('qiyipuzong.mat');

改写为:

s1=cell2mat(struct2cell(load('qiyipuzong.mat')));

得到的s1即为矩阵。


一、读取文本内容

test.txt

1 a aa aaa

2 b bb bbb

3 c cc ccc

整个文本读取

1)data = load(test.txt);%一般数据类型,可以直接使用

2)

fid   = fopen(file, 'r');

data  = textscan(fid, '%f %s %s %s');%属于cell类型,利用{}转换成一般类型

fclose(fid);

dt(:,1)= data{1};

dt(:,2)= data{2};

dt(:,3)= data{3};

dt(:,4)= data{4};

以行读取文本

fid = fopen(file, 'r');

while ~feof(fid)

   string = fgets(fid);

  str  = string(1:1);

  if ( str ~= '#' ) %这样做的好处,是可以注释一些行,方便数据测试

   str1 = string(1:2);

   str2 = string(4:6);

   str3 = string(8:10);

  end

end

fclose(fid);

二、画图

1)提取矢量图

uimenufcn(gcf,'EditCopyFigure')

2)修改figure格式

text(0.3,y0,['north slope=' num2str(a(2))],'FontSize',18);

ylabel('Power/(mm^2 year)','FontSize',12);

xlabel('Frequency(cycle/year)','FontSize',12);

title(st,'FontSize',15);

set(gca,'xlim',[0 col+1]);

set(gca,'ylim',[0 row]);

hold on

plot([xx xx], get(gca, 'YLim'), '-r', 'LineWidth', 3) % 红色,宽度为3

三、显示格网

grid on

只显示某一个方向的格网

set(gca,'Xgrid','on');

关闭set(gca,'Xgrid','off');  

四、subplot之间的间隔问题

figure;set (gcf,'Position',[100,100,900,600], 'color','w');

%% main

fig_hei=0.28;

fig_wei=0.45;

lef_cor_x=0.065;

lef_cor_y=0.08;

%%

subplot(3,2,1,'position', [lef_cor_x lef_cor_y+2*fig_hei fig_wei fig_hei]);

legend({'GPS Observation', 'Logarithmic function fit','Coseismic epoch'},'fontsize',6);

subplot(3,2,2,'position', [lef_cor_x+fig_wei lef_cor_y+2*fig_hei fig_wei fig_hei]);

subplot(3,2,3,'position', [lef_cor_x lef_cor_y+fig_hei fig_wei fig_hei]);

set(gca,'xtick',[]);

sp3_y_tick=get(gca,'ytick');

sp3_y_tick(end)=[];

sp3_y_tick_label=get(gca,'yticklabel');

set(gca,'ytick',sp3_y_tick,'yticklabel',sp3_y_tick_label);

subplot(3,2,4,'position', [lef_cor_x+fig_wei lef_cor_y+fig_hei fig_wei fig_hei]);

subplot(3,2,5,'position', [lef_cor_x lef_cor_y fig_wei fig_hei]);

subplot(3,2,6,'position', [lef_cor_x+fig_wei lef_cor_y fig_wei fig_hei]);

errorbar(GPS_timeseries.time, var*1000, var_sig*1000, 'ko', 'MarkerFaceColor', 'r', 'LineWidth', 0.5);

五、y轴放置右边

set(gca,'YaxisLocation','right');

六、如何在已有的subplot上面画图

sublist = get(gcf, 'children');

plot(sublist(4), t, cos(t), 'g');

注意:sublist序号是倒过来的,也就是如何有subplot(3,2,n),那么sublist(1)=subplot(3,2,6);sublist(6)=subplot(3,2,1);以此类推

七、实习刻度和标记数值不一致

axis([-3 1 0 50]);

set(gca,'xtick',-3:0.1:1);

xt=get(gca,'xtick');

xt_label=get(gca,'xticklabel');

show=-3:0.5:1;

[tf,loc]=ismember(show,xt);

tloc=1:length(xt);

C=setdiff(tloc, loc);

xt_label(C,:)=' ';

set(gca,'xticklabel',xt_label);

set(gca,'tickdir','out')%刻度向外

box off%关闭上面和右面坐标轴

八、图例横着排列

plot(X_VWE_Xv(x,1),'-rs');

plot(X_VWE_Xv(x,2),'b-o');

plot(x,X_VWE_Xv(x,3),'-blacks');

h = legend('PC1','PC2','PC3');

set(h,'Fontsize',5,'Orientation','horizon','location','South','Box','off')

九、去掉科学计数显示:

format long g



https://wap.sciencenet.cn/blog-858128-1003634.html

上一篇:psvelo
下一篇:xshell使用连接ubuntu系统
收藏 IP: 221.235.64.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (2 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 06:21

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部