# Create a new table from a plot constructed from data points

4 views (last 30 days)
aroj bhattarai on 17 Jan 2022
Edited: aroj bhattarai on 16 Feb 2022
Dear all,
Reading an excel sheet with around 24000 data points, I made a plot. Now I would like to create a new table from this plot with a user-defined interval, let say with only 10 data points. Is it possible to do so in Matlab?
Bhattarai

_ on 17 Jan 2022
% a table with 1000 rows:
t = table((1:1000).',2*(1:1000).'+100,'VariableNames',{'x' 'y'})
t = 1000×2 table
x y __ ___ 1 102 2 104 3 106 4 108 5 110 6 112 7 114 8 116 9 118 10 120 11 122 12 124 13 126 14 128 15 130 16 132
% user-defined interval over x:
x_limits = [200 209];
% logical index saying whether each x in the table is within the interval:
idx = t.x >= x_limits(1) & t.x <= x_limits(2);
% new table with x and y but only where x is within the interval:
new_t = table(t.x(idx),t.y(idx),'VariableNames',{'x' 'y'})
new_t = 10×2 table
x y ___ ___ 200 500 201 502 202 504 203 506 204 508 205 510 206 512 207 514 208 516 209 518
aroj bhattarai on 18 Jan 2022
Thank you Benjamin for your interest on my question. But I guess my writing confused you for my objective. Your answer seems to extract specific (X,Y) data points from the given lumpsum data points and to create a new table out of it.
However, my objective is different. As shown in the attached figure A), I have a plot constructed from around 24000 (X,Y) data points where X runs from 1.0 to 1.3987 with data values as:
1.0000, 1.0236, 1.0256, 1.0287, ........, 1.0358, 1.0359, ....., 1.0579, 1.0581, 1.0596, 1.6113, .............., 1.3726, 1.3862, 1.3889, 1.3961, 1.3987.
As shown in figure B), my objective is to assign a user-defined interval/gap/step/space, for example 0.05 in the linear space of X data points in the plot. And from the plot, I would like to extract only 9 (X,Y) data points to be written in a new table, i.e., (1.00, Y1), (1.05, Y2), (1.10, Y3), (1.15, Y4), (1.20, Y5), (1.25, Y6), (1.30, Y7), (1.35, Y8), (1.3987, Y9).
Bhattarai

aroj bhattarai on 16 Feb 2022
Edited: aroj bhattarai on 16 Feb 2022
Although non-profesionally coded, sharing the desired answer in case somebody new in MATLAB have similar issue:
Reading the data set from the excel file
[~, ~, raw] = xlsread('C:\Users...\ABC.xlsx','PQR'); % read PQR sheet of ABC excel file
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
cellVectors = raw(:,[1,11]);
raw = raw(:,[1,2,3,4,5,6,7,8,9,10,11]);
% 8th and 10th column are input X and Y dataset with X = [1.0 1.3987]
Laminp = data(:,8);
Siginp = data(:,10);
Tabulating the dataset
v = table;
llim=1;
ulim=24000;
v.lam = Laminp(1:24000);
v.sig = Siginp(1:24000);
clearvars data raw cellVectors R Laminp Siginp;
lamint(:,1) = v.lam;
Sigint(:,1) = v.sig;
Interpolating the dataset at desired X interval
t = table;
t.lamtab = lamint(:,1); % data acquired from another excel file tabulated in new table t
t.Sigtab = Sigint(:,1);
Lam_coarse = linspace(1, 1.3987, 10); % Coarse X dataset
Sig_coarse = interp1(t.lamtab,t.Sigtab,Lam_course); % Coarse Y dataset
xlswrite('C:\Users...\XYZ.xlsx',Lam_coarse','Data','A1'); % in the 1st column of Data sheet
xlswrite('C:\Users...\XYZ.xlsx',Sig_coarse','Data','B1'); % in the 2nd column of Data sheet
Plotting
figure(1)
plot(lamint(:,1),Sigint(:,1),'o','LineWidth',1.0, 'color','b');
hold on
plot(Lam_coarse,Sig_coarse,'-','LineWidth',1.0, 'color','b');
grid on
hold off