Sampling time of matlab figure data

8 views (last 30 days)
SM
SM on 3 Oct 2024
Answered: Aniket on 4 Oct 2024
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.
  2 Comments
Aniket
Aniket on 4 Oct 2024
Hi @SM, can you please clarify if you want to extract data from saved figure and resample it to 0.1 OR do you want to save scope data to MATLAB fig with sample rate of 0.1 ?
SM
SM on 4 Oct 2024
Yes, I want to extract data from saved figure and resample it to 0.1.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 4 Oct 2024
hello
to retrieve data from a figure , you can do it by two ways :
  • for recent releases :
openfig('data points.fig');
set(gcf, 'Visible','off')
Ax = gca; % Another Option
Lines = findobj(Ax, 'Type','line'); % Return Handles To All 'Line' Objects
x = Lines.XData(:); % Independent Variable (Force Column Vector With '(:)')
y = Lines.YData(:); % Dependent Variable (Force Column Vector With '(:)')
Figure_Table = table(x,y, 'VariableNames',{'X values','Y values'})
  • for older releases
data = extract_data_from_figures('data points.fig'); % uses function extract_data_from_figures below
VN = data.names; % variable names
data = data.Y;
x = data(:,1);
y = data(:,2);
figure(1)
plot(x,y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data = extract_data_from_figures(filename)
%%
%
% Input : File name <filename.fig>
% with multiple plots in a single file
% Output : struct data
% : data.names contains names of the display object Yvalues
% : data.Y contains the actual plot values withthe first column
% containing the x-values
%
% Written by Chetanya Puri, 2019
% Last Modified: Nov 6, 2019
%
fig = openfig(filename); % Open figure and assign it to fig object
dataObjs = findobj(fig,'-property','YData'); % Find all graphic objects with YData, in our case line values
xval = dataObjs(1).XData; % Find the X-axis value
Ymat = [xval(:)]; % Create a matrix with first column of x values
for i=1:length(dataObjs)
legend_name{i,1} = dataObjs(i).DisplayName;
yval = dataObjs(i).YData;
Ymat = [Ymat yval(:)]; % Keep appending column vectors
end
close(fig); % close the figure
data.names = ['X';legend_name];
data.Y = Ymat;
end
once you have data in your workspace you can use decimate or resample to change the rate as you like
those functions requires the Signal Processing Tbx
If you don't have it , you can make a crude downsampling by taking every k samples of the original data (would normally require that you first low pass your data to avoid aliasing)

More Answers (1)

Aniket
Aniket on 4 Oct 2024
Hi @SM,
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!

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!