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

3,736 ビュー (過去 30 日間)

古いコメントを表示

MathWorks Support Team 2013 年 7 月 10 日

  • リンク

    この質問への直接リンク

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

  • リンク

    この質問への直接リンク

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

コメント済み: Walter Roberson 2022 年 4 月 22 日

採用された回答: 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.

サインインしてこの質問に回答する。

採用された回答

MathWorks Support Team 2020 年 11 月 11 日

  • リンク

    この回答への直接リンク

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

  • リンク

    この回答への直接リンク

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

編集済み: MathWorks Support Team 2020 年 11 月 5 日

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.

2 件のコメント

なしを表示なしを非表示

Walter Roberson 2015 年 5 月 8 日

このコメントへの直接リンク

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

  • リンク

    このコメントへの直接リンク

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

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

xdata = get(lineObjs, 'XData');

Walter Roberson 2017 年 12 月 4 日

このコメントへの直接リンク

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

  • リンク

    このコメントへの直接リンク

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

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.

サインインしてコメントする。

その他の回答 (2 件)

Felipe Bittencourt de Souza 2017 年 12 月 15 日

  • リンク

    この回答への直接リンク

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

  • リンク

    この回答への直接リンク

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

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');

0 件のコメント

-2 件の古いコメントを表示-2 件の古いコメントを非表示

サインインしてコメントする。

Yair Altman 2018 年 5 月 21 日

  • リンク

    この回答への直接リンク

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

  • リンク

    この回答への直接リンク

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

編集済み: MathWorks Support Team 2021 年 4 月 19 日

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.

2 件のコメント

なしを表示なしを非表示

Walter Roberson 2022 年 4 月 20 日

このコメントへの直接リンク

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

  • リンク

    このコメントへの直接リンク

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

@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 2022 年 4 月 22 日

このコメントへの直接リンク

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

  • リンク

    このコメントへの直接リンク

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

[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.

サインインしてコメントする。

サインインしてこの質問に回答する。

参考

カテゴリ

MATLABGraphicsGraphics ObjectsGraphics Object Programming

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

  • extract
  • data
  • figure
  • fig
  • line

製品

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

エラーが発生しました

ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。


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

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

Web サイトの選択

Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:

また、以下のリストから Web サイトを選択することもできます。

南北アメリカ

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

ヨーロッパ

  • 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)

アジア太平洋地域

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

最寄りの営業オフィスへのお問い合わせ

How do I extract data from MATLAB figures? (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6280

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.