Extracting data from figures

10 vues (au cours des 30 derniers jours)
Cheggers
Cheggers le 21 Oct 2021
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.
Above are two examples of the figures.
Overlaid they look like this
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
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.

Réponses (1)

Cris LaPierre
Cris LaPierre le 22 Oct 2021
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
X = 1×11
1 2 3 4 5 6 7 8 9 10 11
Y = ln1.YData
Y = 1×11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by