How can I extract data from the following Matlab figure, there are multiple lines.

11 views (last 30 days)
alpha = -0.1;
sigma = 0.1;
eps = -0.1;
e = 0.2;
M = 4;
a = 2;
psi_list = [10, 20, 30, 40, 50];
lambda = 0:0.2:4;
for i = 1:numel(psi_list)
psi = psi_list(i);
hbar = @(x) a - a.*x + x;
A1 = eps + alpha^3 + (3 * sigma^2 * alpha);
B1 =@(x,lambda) (-3 * lambda * M) * ((hbar(x).^2) + (2 .* hbar(x) .* alpha) + (sigma^2) + (alpha^2));
a1 =@(x) tanh(M .* hbar(x));
b1 =@(x) 1 - ((tanh(M .* hbar(x))).^2);
c1 = (M * alpha) - ((M^3 * A1)/3);
d1 = @(lambda) 2 * (M^2) * (1 + lambda);
C1=@(x) a1(x) + (b1(x) .* c1);
D1 =@(x,lambda) d1(lambda) .* ((hbar(x).^3) + (3 .* (hbar(x).^2) .* alpha) + (3 .* hbar(x) .* (alpha)^2) + (3 .* hbar(x) .* (sigma)^2) + eps + (3 * alpha * (sigma^2)) + (alpha^3));
f1 =@(x,lambda) B1(x,lambda) + (3 * lambda .* C1(x) .* hbar(x)) + (3 * lambda .* C1(x) .* alpha) + (D1(x,lambda) .* C1(x));
f2 =@(x,lambda) 12 * (M^2) * (1 + lambda) .* C1(x);
f3 = psi * (e^3);
f4 = @(lambda) (1 + lambda) *180 * ((1 - e)^2);
f5 =@(lambda) 1./(2 + lambda);
F = @(x,lambda) ((f5(lambda) .* f1(x,lambda))./f2(x,lambda)) + (f3./f4(lambda));
q1 =@(x,lambda) hbar(x) ./ (2 .* F(x,lambda));
Q1 = @(lambda)integral(@(x)q1(x,lambda),0,1,'ArrayValued',true); % integration with respect to "x" limit 0 to 1
q2 = @(x,lambda) 1./(F(x,lambda));
Q2 = @(lambda)integral(@(x)q2(x,lambda),0,1,'ArrayValued',true); % integration with respect to "x" limit 0 to 1
Q = @(lambda) Q1(lambda)./Q2(lambda); % Q is a function of "lambda"
A2 = eps + alpha^3 + (3 * sigma* alpha^2 );
c2 = (M * alpha) - ((M^3 * A2)/3);
C2=@(x) a1(x) + (b1(x) .* c2);
dP = @(x,lambda) ((0.5 .* hbar(x)) - Q(lambda)) ./ F(x,lambda);
k1 = @(x,lambda) ((hbar(x) + alpha)./2) .* dP(x,lambda);
k2 = @(lambda) M .* (1 + lambda) .* (2 + lambda);
k3 = @(x,lambda) 4 * M .* hbar(x) .* (hbar(x) + alpha) .* (1 + lambda) + (lambda .* C2(x));
fri = @(x, lambda) k1(x,lambda) + ((k2(lambda)) ./ k3(x,lambda));
Fri =@(lambda) integral(@(x)fri(x,lambda),0,1,'ArrayValued',true);
plot(lambda,Fri(lambda))
hold on
end
legend(num2str(psi_list'))
ylim([0 1])
xlim([0 4])
set(gca, 'ytick', 0:0.1:1);
set(gca, 'xtick', 0:0.2:4);
xlabel('lambda')
ylabel('Fri(lambda)')

I have tried the following code but this is not working.

open('example.fig');
a = get(gca,'Children');
xdata = get(a, 'XData');
ydata = get(a, 'YData');

Accepted Answer

dpb
dpb on 12 Jul 2022
Edited: dpb on 12 Jul 2022
Already you have them in
plot(lambda,Fri(lambda))
if you'll just save
...
y(:,i)=Fri(lambda);
hL(i)=plot(lambda,y(:,i));
"x" is lambda and is fixed all elements.
With the above in the loop (preallocate 'y' before the loop for efficiency, of course), then you could move
plot(lambda,Fri(lambda))
hold on
out of the loop and instead
hL=plot(lambda,y);
...
using the vectorized operations of plot to treat each column of an array as a separate variable.

More Answers (2)

KSSV
KSSV on 12 Jul 2022
Already you have them in xdata and ydata.
They are cell arrays, you can access them by:
xdata{1}
xdata{2}
ydata{1}
ydata{2}

Steven Lord
Steven Lord on 12 Jul 2022
One easy way to do this is to store the handles to the lines as you plot them. Before your loop preallocate an array to hold the handles:
psi_list = [10, 20, 30, 40, 50];
h = gobjects(size(psi_list));
Inside the loop, assign the output of plot into h(i). You may also want to set its DisplayName property, which would simplify your legend call to just legend show.
h(i) = plot(lambda,Fri(lambda), 'DisplayName', string(psi));
% later
legend show % Use the DisplayName properties of the lines
Then when you want to retrieve the data from one of the lines, for example line 2:
xx = h(2).XData;
yy = h(2).YData;

Community Treasure Hunt

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

Start Hunting!