How to extract data from cdfplot()

6 次查看(过去 30 天)
Hello, allow me inqure something that might be simple but is proving to be a challenge to me. I have this code attached. I would like to extract data from the cdfplot() but I keeop getting errors (error attached below) even after reoving the inf at the end. What can be the problem and how can I avoid it?
A =10*rand(100000,1);
figure()
cdfplot(A)
H = get(gca, 'Children');
x = get(H, 'XData');
y = get(H, 'YData');
xr = 1.24
yr = interp1(x, y, xr)
Error using griddedInterpolant
The coordinates of the input points must be finite values; Inf and NaN are not permitted.
Error in interp1 (line 151)
F = griddedInterpolant(X,V,method);
Error in Epr7errorfunc (line 10)
yr = interp1(x, y, xr)

采纳的回答

Drishti
Drishti 2024-10-9
Hi Okoth,
While reproducing the provided code on my end, I encountered the same issue ‘Coordinates of input point must be finite’.
This can be resolved by removing the ‘Inf’ and ‘NaN’ values from the input points. You can refer to the code snippet below to understand how to remove ‘Inf’ and ‘NaN’ values.
% Remove Inf and NaN values
finiteMask = isfinite(x) & isfinite(y);
x = x(finiteMask);
y = y(finiteMask);
Additionally, make sure that the values are unique by following the implementation outlined below to maintain the uniqueness property.
% Ensure x values are unique by averaging y values for duplicate x values
[xUnique, ~, ic] = unique(x);
yUnique = accumarray(ic, y, [], @mean);
xr = 1.24;
yr = interp1(xUnique, yUnique, xr);
For more information, refer to the MATLAB Documentation of ‘cdfplot’ and ‘accumarray’ function.
I hope this helps in getting started.

更多回答(1 个)

Shreeya
Shreeya 2024-10-8
Hello
The error suggests that either zeros/NaN values are being generated in the code due to an arithmetic operation. Applying appropriate conditions to avoid such data points should help mitigate the issue.
Refer to the below MATLAB Answer to understand all the possible reasons resulting in NaN values to help avoid them:

Community Treasure Hunt

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

Start Hunting!

Translated by