フィルターのクリア

How can I com[act my script by making it run up to an nth variable

21 ビュー (過去 30 日間)
Rowan
Rowan 約8時間 前
回答済み: Voss 約7時間 前
I am looking to plot multiple curves in 3D, the curves consist of 3 variables each; xn, kn and zn, such that the first curve is plotted from x1, k1 and z1. The way i am plotting the curves in 3D can be seen below and hopefuly this provides good context asto what im doing.
I have written a script that i intend to use on multiple sets of curves with varying numbers of curves, the problem parts are;
z1=rot90(v1);
z2=rot90(v2);
z3=rot90(v3);
z4=rot90(v4);
z5=rot90(v5);
z6=rot90(v6);
z7=rot90(v7);
and
plot3(x1, k1, z1, 'linewidth', 2, 'color', 'k');
plot3(x2, k2, z2, 'linewidth', 2, 'color', 'k');
plot3(x3, k3, z3, 'linewidth', 2, 'color', 'k');
plot3(x4, k4, z4, 'linewidth', 2, 'color', 'k');
plot3(x5, k5, z5, 'linewidth', 2, 'color', 'k');
plot3(x6, k6, z6, 'linewidth', 2, 'color', 'k');
plot3(x7, k7, z7, 'linewidth', 2, 'color', 'k')
grid on
surf([x1;x2], [k1;k2], [z1;z2]);
surf([x2;x3], [k2;k3], [z2;z3]);
surf([x3;x4], [k3;k4], [z3;z4]);
surf([x4;x5], [k4;k5], [z4;z5]);
surf([x5;x6], [k5;k6], [z5;z6]);
surf([x6;x7], [k6;k7], [z6;z7]);
is there a way i can get this script to create a variable called zn by rotation a variable called vn up to the nth vn variable? Is there also a similar thing i can do for plotting and drawing the surfaces as right now i am stuck with editing the script each time i am changing the number of curves. I have zero experience with coding so hopefully i have framed this well enough for it to be understandable and sorry if its just trivial.
Thanks in advance.

回答 (2 件)

Steven Lord
Steven Lord 約8時間 前
Can you dynamically create variables with numbered names like z1, z2, z3, etc.? Yes.
Should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.

Voss
Voss 約7時間 前
Use cell arrays to store those things:
v = {v1,v2,v3,v4,v5,v6,v7};
x = {x1,x2,x3,x4,x5,x6,x7};
k = {k1,k2,k3,k4,k5,k6,k7};
Add things as needed.
Then you can use indexing to do what you want, and this part of the code doesn't need to change when things are added to or removed from the v, x, k cell arrays:
n = numel(v);
z = cell(1,n);
for ii = 1:n
z{ii} = rot90(v{ii});
end
for ii = 1:n
plot3(x{ii}, k{ii}, z{ii}, 'linewidth', 2, 'color', 'k');
end
grid on
for ii = 1:n-1
surf([x{ii};x{ii+1}], [k{ii};k{ii+1}], [z{ii};z{ii+1}]);
end

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by