Extract scattered data from fig

조회 수: 6 (최근 30일)
mariana mahamud
mariana mahamud 2019년 5월 13일
댓글: Adam Danz 2019년 5월 14일
I want to extract data from a lot of graph fig files. My data are consist of scattered points in a graph. I used the code below however there is nothing in the x and y variables. How to fix this?
clc
clear all
addpath(genpath('PIV TEST 2'))
for k = 2:21
h = openfig(sprintf('%d.fig',k));
h = findobj(gca,'Type','line');
x = get(h,sprintf( 'XData',k));
y = get(h, sprintf('YData',k));
end
Is there anything wrong in my coding?
a.png
  댓글 수: 2
Adam Danz
Adam Danz 2019년 5월 13일
Are the figures opening? Do you get any error or warning messages? Have you tried running the loop through manually and looking at what happens on each line? What have you done so far in the problem solving phase?
These two lines are definitely wrong.
x = get(h,sprintf( 'XData',k)); % should be get(h, 'XData')
y = get(h, sprintf('YData',k)); % same
Also, you're not storing the loop variables anywhere. They are being overwritten on each loop.
Lots of stuff to fix here.
mariana mahamud
mariana mahamud 2019년 5월 14일
Thank you sir for pointing out my mistake in the loop there was no error at all, all the figures appear well, its the same for manually and loop. The figures shows up like the image i attached but there is no data in the variables

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 5월 14일
편집: Adam Danz 2019년 5월 14일
Here is a cleaned-up version of your code that finds any and all objects in the specified figure that contain an "XData" property. It will work no matter how the plots were produced (line, scatter, etc).
I suggest storing the (x,y) values in a cell array.
x = cell(20,1);
y = x;
for k = 2:21
fh = openfig(sprintf('%d.fig',k));
h = findobj(fh,'-Property', 'XData'); %any object with property "XData" on figure fh
x{k} = get(h,'XData');
y{k} = get(h,'YData');
end
If more than one object was located, x and y will be a cell array containing one element for each object.
  댓글 수: 2
mariana mahamud
mariana mahamud 2019년 5월 14일
I tried this and it works! thank you so much sir!
Adam Danz
Adam Danz 2019년 5월 14일
Glad I could help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by