Opening multiple figures from directory and extracting X and Y data simultaneously

조회 수: 5(최근 30일)
I have been using this code to open multiple figures from a directory How to open multiple fig files at once through code? - ( But I also want to extract X and Y data to find a threshold when Y is between some value such as 0.7 and 1.0 as each figure opens. Here is what I have so far:
[figures, path] = uigetfile('directory', '*.fig', 'Multiselect', 'on');
for n = 1:length(figures)
Multi_Figs = [path, filesep,figures{n}];
Op = openfig(Multi_Figs,'invisible');
h = findobj(gca, 'Type', 'line');
x = get(h, 'Xdata');
y = get(h,'Ydata');
ii = find(0.7 < y < 1.0,1);
x_0 = x(ii);
I know I may have to use a while loop to find the threshold, but I am struggling a bit as to how to implement that when figures are opened simultaneously.
Thank you,

채택된 답변

Dave B
Dave B 2022년 3월 26일
편집: Dave B 2022년 3월 26일
The problem that jumps out in this code is:
ii = find(0.7 < y < 1.0,1);
This makes sense mathematically, but it's not valid MATLAB code. If you want the values in a range you should use syntax like this:
x = linspace(0,1,100);
y = x.*rand(1,100) * 2; % just some fake data to keep things interesting
ii = find(y > 0.7 & y < 1, 1);
In other words: specify each 'condition' and join them together with & (i.e. y is greater than 0.7 AND y is less than 1)
To be clear, using find(..., 1) will retrieve the first value that's in the specified range, if you wanted all the values in that range, you can just skip the find alltogether:
x_in_range = x(y > 0.7 & y < 1);
Let's see it all on a plot just in case anything was unclear:
hold on
h2=plot(x_in_range, y(y > 0.7 & y < 1), 'o', 'MarkerFaceColor','r', 'DisplayName', 'All Hits');
xline(x(ii),'-r','First Hit')
yline([.7 1])
legend([h1 h2],'Location','northwest')

추가 답변(0개)


Find more on Loops and Conditional Statements in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by