- https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
- https://www.mathworks.com/help/matlab/cell-arrays.html
How to include this interpolation in a "for" loop?
11 次查看(过去 30 天)
显示 更早的评论
Hi,
I would like to include this interpolation in a "for" loop. I have a number of sets of data and I would like to make a "for" loop for their interpolation. Can anyone please help me in making the loop?
x1 = [1 4 8 10];
y1 = [1 2 6 7];
z1 = [2 3 5 9];
x2 = [2 6 8 9]
y2 = [5 9 7 6]
z2 = [3 4 7 1]
.
.
.
xn = [ ]
yn = [ ]
zn = [ ]
w1 = interp1(x1,y1,5,'linear')
plot(x1,y1,'-',5,w,'*')
w2 = interp1(x2,y2,5,'linear')
plot(x2,y2,'-',5,w,'*')
.
.
.
wn = interp1(xn,yn,5,'linear')
plot(xn,yn,'-',5,w,'*')
0 个评论
采纳的回答
OCDER
2017-10-17
First, you have to rename all your variables. Labeling your variables x1,x2,x3 ...xn makes it very difficult to use a for loop. This is a fairly common problem that could be fixed using cell arrays. Read:
If you have 1000's of these variables... here's a solution that fixes that:
Best is to start by storing data in cells (use the find and replace option to fix x1 to x{1}, etc:
x{1} =
y{1} =
z{1} =
x{2} =
y{2} =
z{2} =
Then you can use for loops!
w = cell(size(x));
for k = 1:length(x)
w{k} = interp1(x{k},y{k},5,'linear')
plot(x{k},y{k},'-',5,w{k},'*')
if k == 1 %In case you want to plot over many times
hold on
end
end
3 个评论
Stephen23
2017-10-31
You could also use cellfun:
>> X = {[1,4,8,10],[2,6,8,9]};
>> Y = {[1,2,6, 7],[5,9,7,6]};
>> fun = @(x,y)interp1(x,y,5,'linear');
>> C = cellfun(fun,X,Y,'uni',0)
this makes it simple to put all of the output values into one array and then plot it.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!