%% ============ How to Extract Data from MATLAB '.fig' File =========== %%
% Methods to extract data from MATLAB '.fig' file in different situations.
% Reference: https://blog.csdn.net/yangziluomu/article/details/39520771  
% Provider: Xu-Zhen ZHENG
% Date: Dec 18, 2024

%% Extraction of Pixel Data from an Image
% If your .fig file contains an image where each pixel has data,
% you can obtain the value for each point on the image by:
open('figname.fig');
h = get(gca, 'Children');        % Get handle to figure children
data = get(h, 'Cdata');          % Extract pixel data (matrix corresponding to image pixels)

%% Extraction of a Single Curve
% If your .fig file contains a single curve, such as from the 'plot' command,
% you can extract the x and y coordinates as follows:
open('figname.fig');
h_line = get(gca, 'Children');   % Get handle to the plot line
xdata = get(h_line, 'Xdata');    % Extract X data
ydata = get(h_line, 'Ydata');    % Extract Y data

%% Extraction of a Three-Dimensional Single Curve
% If your .fig file contains a 3D curve, such as from the 'plot3' command,
% you can extract the x, y, and z coordinates as follows:
open('figname.fig');
h_line = get(gca, 'Children');   % Get handle to the plot line
xdata = get(h_line, 'Xdata');    % Extract X data
ydata = get(h_line, 'Ydata');    % Extract Y data
zdata = get(h_line, 'Zdata');    % Extract Z data

%% Extraction of Multiple Curves
% If your .fig file contains multiple curves (e.g., from multiple 'plot' commands),
% you can extract the x and y coordinates for each curve as follows:
open('figname.fig');
lh = findall(gca, 'type', 'line'); % Find all line objects in the current axis
xc = get(lh, 'Xdata');              % Extract x-axis data (cell array for multiple curves)
yc = get(lh, 'Ydata');              % Extract y-axis data (cell array for multiple curves)

% To extract the x and y coordinates of the second curve:
x2 = xc{2};
y2 = yc{2};
