Extracting data from figures
1 回表示 (過去 30 日間)
古いコメントを表示
So i have a set of code that creates a bunch of figures for me. I would then like to extract the data from said figures.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/774243/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/774248/image.png)
Above are two examples of the figures.
Overlaid they look like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/774253/image.png)
These figures are origionally plotted using
function plotSpectrum(h,x,y,scale,colour)
% Deal with variable arguments
if nargin < 5 colour='black'; end
if nargin < 4 scale=1; end
% Plot
p=plot(h,x,real(y));
xlimits=[min(x),max(x)];
ylimits=[min(real(y)),max(real(y))/scale];
% Format
set(p,'color',colour,'LineWidth',1);
set(h,'xlim',xlimits,'ylim',ylimits,'xdir','reverse');
and overlaid using
fh1 = open('1hydrogen.fig');
fh2 = open('2hydrogen.fig');
ax1 = get(fh1, 'Children');
ax2 = get(fh2, 'Children');
ax2p = get(ax2(1),'Children');
copyobj(ax2p, ax1(1));
So i then try to extract the data
data = pwd;
dinfo = dir(fullfile(data,'*gen.fig'));
fignames = {dinfo.name};
numfig = length(fignames);
y = cell(numfig, 1);
z = cell(numfig, 1);
inter_y = cell(numfig, 1);
inter_z = cell(numfig, 1);
for K = 1 : numfig
figfile = fignames{K};
try
fig = openfig(figfile,'invisible');
ax = get(fig, 'CurrentAxes');
if ~isempty(ax)
hline = get(ax, 'Children');
y{K} = get(hline,'XData');
z{K} = get(hline,'YData');
inter_y{K} = y{K}(1,5:124);
inter_z{K} = abs(z{K}(1,5:124));
end
close(fig);
end
end
Then if i use something to test this extraction like.
test = inter_z{1,1};
test2 = inter_z{2,1};
testx = 1:120
plot (testx,test)
hold on
plot (testx,test2)
I get
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/774258/image.png)
Which is clearly different to the other overlay example. I am trying to get it so that my extracted data looks the same when plotted as the origional as this is needed for later coding.
Any help would be appreciated.
0 件のコメント
回答 (1 件)
Cris LaPierre
2021 年 10 月 22 日
You have the code that creates the figures, but you want to extract the data from the figure? Why? Don't you already have the data captured in the variables used to create the plot?
Still, you can do the following. The figure contains a child, which is the axes. You axes contain 2 children, the two lines. Each line has an XData and YData property. You can acces the data from the figure that way.
% Create a figure
plot(0:0.1:1);
% Extract X and Y data
fh = gcf;
ax = fh.Children(1);
ln1 = ax.Children(1);
X = ln1.XData
Y = ln1.YData
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!