naming the figure in loop

10 ビュー (過去 30 日間)
sanket neharkar
sanket neharkar 2022 年 5 月 21 日
コメント済み: Jan 2022 年 5 月 23 日
i had written a code for 100 points and to have a leasr square fit i had too first 5 points and jiust slided it till the end now for that i lave lmited the loop till 95 and plotted a least cureve fit now i have to plot the last 5 points in a single plot how should i do that and also i have done a looping of first 5,7,9 points so i amgetting 3 graph now i have to name the 3 figures how can i do that
  5 件のコメント
sanket neharkar
sanket neharkar 2022 年 5 月 23 日
function phi = curvefit3(x,y)
clear all;
close all;
clc;
[data, ~] = xlsread('filename.xlsx');
x_imp = data(:,1); %%reading rows and first column
y_imp = data(:,2); %%reading rows and second column
%figure;
[rows, ~] = size(data);
for i = 5:2:9
window = i;
fig(i) = figure('name', sprintf('%d', i),'NumberTitle','off');
for i = 1: 1 : rows -window %%loop to read i+1 to i+5 rows
x = x_imp(i:i+window-1,1);
y = y_imp(i:i+window-1,1);
x0 = x(1);
var1 = x - x0;
var2 = var1.*var1;
%% construction of the least-squares quadratic fit to the data
%% we use the equation y =a0 + a1t + a2t^2 %%t = (x-x0)
%% numbers a0,a1 and a2 are the unknowns
x_mat = [ones(window,1) var1 var2];
amat = x_mat'*x_mat;
bmat = x_mat'*y;
phi = inv(amat)*bmat;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
error = y-(x_mat*phi);
marker = floor(window/2) + 1;
if (i == 1)
err(1:marker,1) =error(1:marker,1);
else
err(i+marker-1) =error(marker);
end
pos_n = (a0+a1*var1+a2*var2);
markar = floor(window/2) + 1;
if (i == 1)
pos(1:markar,1) = pos_n(1:markar,1);
else
pos(i+markar-1) = pos_n(markar);
end %if (i == 1)
velo = diff(pos);
acc_n = diff(diff(pos));
%plot(velo);
end %for i = 1: 1 : 20-5
n= length(pos);
tim_pos = x_imp(1:n);
tim_velo = x_imp(1:n-1);
tim_acc_n = x_imp(1:n-2);
y_raw = y_imp(1:n);
plot(tim_pos,y_raw,'or',tim_pos,pos','-g',tim_velo,velo','--k',tim_acc_n,acc_n','*b'); grid;
legend(' raw data', 'Curve Fit',' velocity', 'acceleration');
xlabel(' Time(sec) ');
ylabel(' Function ');
figure;
plot(tim_pos,err,'--k');grid;
legend('error1');
end %for i = 5:2:9
this is a code which i have written and in this i had named the figures accordingly with help of your ans,but now with this code i am getting 3 error plot in 3 figure i want to plot all in one figure how should i do that.
and also i have made the graph for the points in which i have left the no.of points = window that means i have left the last 5,7,9 oints from the data and plotted it nnow i want to join that last points in the graph
how the code should be written or updated inorder to do that
Jan
Jan 2022 年 5 月 23 日
@sanket neharkar: Please format your code uing the tools above the section for editing. Make your question as readable as possible.
Starting a function with clear all; is a waste of time only. This clears all variables of the local workspace, but there are non at the first line of a function. In addition it removes all loaded functions from the memory. Reloading them from the slow disk wastes time only. Therefore "clear all" is "cargo cult programming" and an anti-pattern.
Do not use "error" as a name of a variable, becuase this is an important Matlab function, which cannot be called anymore afterwards.
You want to draw all graphs to the same figure. Then do not create 3 figures by:
fig(i) = figure('name', sprintf('%d', i),'NumberTitle','off');

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2022 年 5 月 22 日
編集済み: Image Analyst 2022 年 5 月 22 日
The Savitzky-Golay filter is a sliding polynomial fit. You can set up a window width of 5 elements and slide it along your vector fitting each set of 5 numbers to a polynomial of order 1, 2, 3, or 4. The sgolayfilt() function in the Signal Processing Toolbox does this for you. You just give it your data, the window width you want, and the order of the polynomial and it returns the filtered output vector.
I'm attaching the 3 sgolayfilt() demos I have. Adapt as needed.
To name figures in a loop you can do this
for k = 1 : 3
hFig(k) = figure;
hFig(k).Name = sprintf('This is my figure #%d', k);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSignal Generation and Preprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by