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

11 views (last 30 days)
Hello,
I have been using this code to open multiple figures from a directory How to open multiple fig files at once through code? - (mathworks.com). 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);
end
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,
Daynah

Accepted Answer

Dave B
Dave B on 26 Mar 2022
Edited: Dave B on 26 Mar 2022
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);
x(ii);
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:
h1=plot(x,y,'DisplayName','Data','LineWidth',2,'Marker','o','MarkerFaceColor','w');
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')

More Answers (0)

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!