- 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?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ismail Qeshta
el 17 de Oct. de 2017
Comentada: Ismail Qeshta
el 1 de Nov. de 2017
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 comentarios
Respuesta aceptada
OCDER
el 17 de Oct. de 2017
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 comentarios
Stephen23
el 31 de Oct. de 2017
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!