How do I extract data from MATLAB figures? (2024)

3 775vues (au cours des 30derniers jours)

Afficher commentaires plus anciens

MathWorks Support Team le 10 Juil 2013

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures

Commenté: Walter Roberson le 22 Avr 2022

Réponse acceptée: MathWorks Support Team

I have a few MATLAB figures, but no MATLAB code associated with it. I want to extract the data from the curves in the figures.

Connectez-vous pour répondre à cette question.

Réponse acceptée

MathWorks Support Team le 11 Nov 2020

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#answer_110036

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#answer_110036

Modifié(e): MathWorks Support Team le 5 Nov 2020

This example shows how to extract data from a MATLAB figure.

If the figure is stored in a file, such as 'example.fig', then open the figure file using 'openfig'. Assign the Figureobject to the variable 'fig'.

fig = openfig('example.fig');

If the figure is already open, then use 'gcf'to access the Figure object and assign it to the variable 'fig'.

fig = gcf;

There are several ways to access the data for the plotted graphics objects. You can use the Childrenproperty or you can use 'findobj'.

Use Children Property

Access the plotted graphics objects through the Childrenproperties. The Axesobjects are children of the figure. The plotted graphics objects are typically children of the Axesobject.

axObjs = fig.Children

dataObjs = axObjs.Children

The 'dataObjs'array that appears in the Command Window indicates the types of graphics objects in the axes. Different graphics objects store data differently. For example, Lineobjects store the data in the 'XData', 'YData', and 'ZData'properties. If the first element in 'dataObjs'is a Lineobject, then access its data using this code.

x = dataObjs(1).XData

y = dataObjs(1).YData

z = dataObjs(1).ZData

If the figure contains other types of graphics objects, then use the appropriate properties to access the data. For a list of graphics objects and their properties, see:

https://www.mathworks.com/help/matlab/graphics-object-properties.html

Use findobj Function

Alternatively, you can find all the graphics objects in a figure with a certain data property. For example, find all graphics objects that have a 'YData'property. Then access the 'YData'values for the first object.

dataObjs = findobj(fig,'-property','YData')

y1 = dataObjs(1).YData

2commentaires

Afficher AucuneMasquer Aucune

Walter Roberson le 8 Mai 2015

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_283780

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_283780

Ouvrir dans MATLAB Online

lineObjs = findobj(dataObjs, 'type', 'line');

xdata = get(lineObjs, 'XData');

Walter Roberson le 4 Déc 2017

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_512527

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_512527

Ouvrir dans MATLAB Online

When you plot a matrix by columns, then the order of handles returned from the plot() call is the order of the columns:

data = sort(rand(20,5),2);

h = plot(data);

Now h(1) corresponds to column 1, h(2) corresponds to column 2, and so on. You can confirm this with:

h(1).DisplayName = 'col1';

h(2).DisplayName = 'col2';

h(3).DisplayName = 'col3';

h(4).DisplayName = 'col4';

h(5).DisplayName = 'col5';

legend();

and see that indeed the item labeled col5 is the one with highest average Y (it was constructed that way by the sort() call).

However, the order of axes children defaults to the reverse of this:

>> get(gca,'Children')

ans =

5×1 Line array:

Line (col5)

Line (col4)

Line (col3)

Line (col2)

Line (col1)

because the rule is that the axes children are (by default) painted from last to first (first is on top, last is on bottom). This can be altered in a few ways, including some obscure specialized settings that were new in R2014b, but also the order can be changed with good old uistack()

When you recall a figure file and pull out the axes children, the axes children are going to be in the same order as was present in the axes when it was saved to the figure file. If nothing in the original code altered the order, that is going to be last column first. So if you retrieve the YData and mat2cell() it into a 2D matrix, make sure to fliplr() to get the original order.

Connectez-vous pour commenter.

Plus de réponses (2)

Felipe Bittencourt de Souza le 15 Déc 2017

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#answer_296330

Ouvrir dans MATLAB Online

I was having the same error message mentioned before: "Error using get Conversion to double from cell is not possible."

I solved this issue with Walter Roberson's answer, using the following code:

open('example.fig');

a = get(gca,'Children');

xdata = get(a, 'XData');

ydata = get(a, 'YData');

zdata = get(a, 'ZData');

0commentaires

Afficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Yair Altman le 21 Mai 2018

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#answer_321146

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#answer_321146

Modifié(e): MathWorks Support Team le 19 Avr 2021

Note that the official MathWorks answer above relies on opening and displaying the figure (using the open() function) before extracting its contents. This is both slow and potentially unwanted (we don't always want to display the figures), especially if we're looping over many FIG files.

Instead, users can directly read and analyze the *.fig file by loading it into Matlab memory using the load() function, since *.fig files are basically simple MAT files with a .fig (rather than .mat) file extension.

Fortunately, the internal format of these files has changed very little over the years - a few fields have changed their name, but the basic file data structure remained the same. So essentially the same code can be used to extract data from .fig files created a decade ago, as well as the latest Matlab release.

Note that the fact that FIG files are basically just MAT files is an undocumented feature of Matlab, and so it might change one day. But for now it is a very handy feature to use.

2commentaires

Afficher AucuneMasquer Aucune

Walter Roberson le 20 Avr 2022

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_2113925

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_2113925

Ouvrir dans MATLAB Online

@Eugene Msizi Buthelezi

fig = openfig('figure.fig');

all_ax = findobj(fig, 'type', 'axes');

all_titles = cellfun(@(T) T.String, get(all_ax, 'title'), 'uniform', 0);

all_lines = arrayfun(@(A) findobj(A, 'type', 'line'), all_ax, 'uniform', 0);

all_XData = cellfun(@(L) get(L,'XData'), all_lines, 'uniform', 0);

all_YData = cellfun(@(L) get(L,'YData'), all_lines, 'uniform', 0);

At this point,

  • all_titles is a cell array of character vectors containing the title for each axes (in latex form)
  • all_XData is a cell array with one entry for each axes, and the entry is a cell array of numeric row vectors, one entry for each line in the axes
  • all_YData is a cell array with one entry for each axes, and the entry is a cell array of numeric row vectors, one entry for each line in the axes

WIth that figure, there are three lines in almost all of the axes, but one of them has four lines (the legend which is attached to one of the axes only has three names defined.)

Walter Roberson le 22 Avr 2022

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_2115905

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures#comment_2115905

Ouvrir dans MATLAB Online

[filename, filepath] = uigetfile('*.fig');

if ~ischar(filename)

error('cancel');

end

fullname = fullfile(filepath, filename);

fig = openfig(fullname);

all_ax = findobj(fig, 'type', 'axes');

all_titles = cellfun(@(T) T.String, get(all_ax, 'title'), 'uniform', 0);

all_lines = arrayfun(@(A) findobj(A, 'type', 'line'), all_ax, 'uniform', 0);

all_XData = cellfun(@(L) get(L,'XData'), all_lines, 'uniform', 0);

all_YData = cellfun(@(L) get(L,'YData'), all_lines, 'uniform', 0);

for axIdx = 1 : numel(all_YData)

if iscell(all_YData{axIdx})

mask = cellfun(@(Y) ~isequal(Y, [0 0]), all_YData{axIdx});

all_XData{axIdx} = all_XData{axIdx}(mask);

all_YData{axIdx} = all_YData{axIdx}(mask);

else

all_XData{axIdx} = {all_XData{axIdx}};

all_YData{axIdx} = {all_YData{axIdx}};

end

end

This code permits you to select a .fig file, and processes it. It outputs a cell array of character vectors named all_titles . It outputs a cell array named all_XData in which there is one celll array entry for each axes, that contains an entry for each line inside the axes, that is the line x coordinates. It outputs a cell array named all_YData in which there is one celll array entry for each axes, that contains an entry for each line inside the axes, that is the line y coordinates. The coordinate entries have been filtered to remove any lines with Y coordinate [0 0]

The difference between this code and the previous version is that this version filters out lines where the y coordinate is just [0 0]. This version also accounts for the possibility that an axes only has one line.

In the case where the axes had more than one line, the internal get() call would have returned a cell array of coordinates, but in the case where the axes had exactly one line, the internal get() call would have returned the numeric coordinates directly: this code detects the single-line case and deliberately wraps it inside a cell array, so that the outputs are consistent.

So, for axes #K,all_titles{K} is a character vector that is the axes title, and all_XData{K} is a cell array with one entry per line inside the axes for the X coordinates, and all_YData{K} is a cell array with one entry per line inside the axes for the Y coordinates.

This code does not assume that all of the lines inside an axes have the same number of points. If you are willing to assume that, then you can process the arrays further by

XData_matrices = cellfun(@cell2mat, all_XData);

YData_matrices = cellfun(@cell2mat, all_YData);

and then those would be cell arrays with one entry per axes, and the entries would be N x L numeric arrays where N is the number of lines and L is the number of points in the line.

Connectez-vous pour commenter.

Connectez-vous pour répondre à cette question.

Voir également

Catégories

MATLABGraphicsGraphics ObjectsGraphics Object Programming

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

Tags

  • extract
  • data
  • figure
  • fig
  • line

Produits

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

Une erreur s'est produite

Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.


Translated by How do I extract data from MATLAB figures? (9)

How do I extract data from MATLAB figures? (10)

Sélectionner un site web

Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .

Vous pouvez également sélectionner un site web dans la liste suivante :

Amériques

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asie-Pacifique

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contactez votre bureau local

How do I extract data from MATLAB figures? (2024)

FAQs

How to extract the data from a MATLAB figure? ›

I have attached the figure, and below is the code:
  1. open('2.fig');
  2. h = gcf; %current figure handle.
  3. axesObjs = get(h, 'Children'); %axes handles.
  4. dataObjs = get(axesObjs, 'Children'); %handles t.
  5. xdata = get(dataObjs, 'XData');
  6. ydata = get(dataObjs, 'YData');
Jan 30, 2019

How to extract results from MATLAB? ›

To save results, select the result in the Test Manager, in the Results and Artifacts pane, and click Export on the toolstrip.
  1. Select complete result sets to export to a MATLAB® data export file ( MLDATX ). Note. ...
  2. Select criteria comparisons or simulation output to export signal data to the base workspace or to a MAT-file.

How do I extract data from MATLAB figure to excel? ›

You can use the "MLGetFigure" function to import current MATLAB figure into Microsoft Excel worksheet. You can also do this by launching Excel from MATLAB by using the ActiveX Automation client feature of MATLAB. You can print a figure to the clipboard and then insert the clipboard contents into Excel.

How to extract data from a plot? ›

You can use an OCR to extract the values and can even try fine-tuning it to capture the symbols. You can correlate the bounding boxes to find the values. Then you can use simple logic to capture the symbols and correlate them on the axis for the value. You can also use an LLM to understand the semantics of the chart.

How to extract data points from an image in MATLAB? ›

Share 'GRABIT'
  1. Load the image file.
  2. Calibrate axes dimensions. You will be prompted to select 4 points on the image.
  3. Grab points by clicking on points. Right-click to delete a point. ...
  4. Multiple data sets will remain in memory so long as the GUI is open. Variables can be renamed, saved to file, or edited in Array Editor.

How do I export MATLAB results to PDF? ›

path = export( file ) converts the specified live script or function to a PDF file with the same name and returns the full path to the converted file. The converted file closely resembles the appearance of the live script or function when viewed in the Live Editor with output inline.

How to read data from MATLAB? ›

Use fopen to open the file, specify the character encoding, and obtain the fileID value. When you finish reading, close the file by calling fclose(fileID) . A = fscanf( fileID , formatSpec , sizeA ) reads file data into an array, A , with dimensions, sizeA , and positions the file pointer after the last value read.

How do I export a figure from MATLAB to CSV? ›

Command: File > Export Data > Raw Data > CSV-MATLAB... This command allows to export the raw data in a format which can be easily read by MATLAB(TM).

How to extract data from Excel to Stata? ›

Using your Windows or Mac computer,
  1. Start Excel.
  2. Enter data in rows and columns or read in a previously saved file.
  3. Highlight the data of interest, and then select Edit and click Copy.
  4. Start Stata and open the Data Editor (type edit at the Stata dot prompt).
  5. Paste data into editor by selecting Edit and clicking Paste.

How to get values from a graph? ›

We can extract the data from the graph by following simple steps:
  1. Step 1: Upload the image to PlotDigitizer. ...
  2. Step 2: Select the graph type. ...
  3. Step 3: Calibrating the axes. ...
  4. Step 4: Extracting data points from the plot. ...
  5. Step 5: Exporting the extracted data.

How to get value from field MATLAB? ›

value = getfield( S , field ) returns the value in the specified field of the structure S . For example, if S.a = 1 , then getfield(S,'a') returns 1 . As an alternative to getfield , use dot notation, value = S. field .

How to generate data in MATLAB? ›

To generate input data, use idinput to construct a signal with the desired characteristics, such as a random Gaussian or binary signal or a sinusoid. idinput returns a matrix of input values. The following table lists the commands you can use to simulate output data.

How to extract text from an image in MATLAB? ›

txt = ocr( I ) returns an ocrText object that contains optical character recognition (OCR) information from the input image I . The object contains recognized characters, words, text lines, the locations of recognized words, and a metric indicating the confidence of each recognition result.

How do I extract a field from a structure in MATLAB? ›

a = extractfield( S , name ) returns the field values specified by the field name of structure S .

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 5916

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.