Sampling time of matlab figure data
Show older comments
I have some real time experiment data saved in matlab figures. While saving the data in simulink scope, the sampling time was set as the inherited sampling time of '-1'. Now, I have those scope data saved as only matlab figures (.fig files). Is it possible to change the sampling time of the saved data to 0.1? Without changing the scope settings and running the real time experiment again? This is required to reduce the number of data points from 200001 to 2001.
Accepted Answer
More Answers (1)
You can extract the data from figure and then resample it. To extract the data, open the figure and use ‘findobj’ function to locate the line objects within figure. Then the data ('XData' and 'YData') can be extracted from these line objects. Find a sample code below:
fig = openfig('test_figure.fig');
lineObjs = findobj(fig, 'type', 'line');
xdata = get(lineObjs, 'XData');
ydata = get(lineObjs, 'YData');
Refer this answer for detailed explanation on extracting data from a figure:
To resample it, first calculate the original sampling time and use ‘resample’ function to get new 'YData'. You can also create a new time vector (x values) if you want to plot the new figure.
original_sampling_time = mean(diff(xdata));
new_sampling_time = 0.1;
[p, q] = rat(original_sampling_time/ new_sampling_time); % Calculate resampling factors
% Resample the yData
new_yData = resample(ydata, p, q);
% Resample the yData
new_yData = resample(ydata, p, q);
new_num_points = length(new_yData);
new_time_vector = (0:new_num_points-1) * new_sampling_time;
figure;
plot(new_time_vector, new_yData);
xlabel('Time (s)');
ylabel('Amplitude');
title('Resampled Data');
savefig('resampled_figure.fig');
Refer to MATLAB documentation on ‘resample’ function for further details:
Hope this answers your query!
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!
