搜档网
当前位置:搜档网 › 直接从fig文件中导出数据的代码

直接从fig文件中导出数据的代码

直接从fig文件中导出数据的代码

alllines = findobj('Type','line');
nlines = length(alllines);
thepoints = cell(nlines,2);
for K = 1:nlines
thepoints{K,1} = get(alllines(K),'XData');
thepoints{K,2} = get(alllines(K),'YData');
end


从Matlab的Figure中导出数据的办法
2011-03-01 22:29
plot成了图,保存成了.fig的文件。现在没有了源码,想把数据导出怎么办?
下面地址给出了一个例子
https://www.sodocs.net/doc/bb9615196.html,/matlabcentral/newsreader/view_thread/154654
具体的办法是:两种方法一样,没什么差别
第一种:https://www.sodocs.net/doc/bb9615196.html,/adda/blog/item/a40aecc40db76cae8326ac69.html
open(fnam); % fnam是文件名
lh=findall(gca,'type','line'); % 从当前图(gca)中取出曲线的handle,
xc=get(lh,'xdata'); % 取出x轴数据,注意,这个x和y是以cell的数据结构保存的
yc=get(lh,'ydata'); % 取出y轴数据
x=xc{1};从cell转换成矩阵,可以这样写
y=yc{1};
第二种:
1、在命令行运行figure打开一个空白窗口,
2、在工具栏或者菜单中选择open打开你保存的fig文件,会显示出原来的那个图来。
3、在命令行运行obj = get(gca,'children')
这个结果对应你的图里面的线或者面的句柄,有几条线就对应几个句柄。
4、根据你需要的是哪条线,第n条线句柄就是obj(n),如果只有一个对象直接用obj就可以,
然后再用一次get,
x=get(obj(1), 'xdata');
y=get(obj(1), 'ydata');
如果是三维图,再加上个z=get(obj(1),'zdata');
x、y、z就是你所需要的坐标数据

-----------------------------
% the data
x=1:10;
y=rand(size(x));
fnam='afig.fig';
% ...on display
fh=figure;
line(x,y);
% ...and saved
saveas(gcf,fnam);
delete(fh);
% the engine
open(fnam);
lh=findall(gca,'type','line');
xx=get(lh,'xdata');
yy=get(lh,'ydata');
% the result
[isequal(x,xx)
isequal(y,yy)]

相关主题