Multiple inputs for one graph
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm trying to figure out a way to attach 3 specific inputs so that they plot different points. Idealy the outputs are all on the same graph, but I'm having trouble with it. I also need to attach it into a GUI which I've never done before.
My curent attempt isn't great. Any help would be appreciated.
disp('Please input a weight and radius for one of the disks.')
UserInputs=input('Please choose a disk (Disk 1, Disk 2, Disk 3):','s')
if UserInputs==('Disk 1')
kg_1=input('Weight: \n');
r_1=input('Radius: \n');
elseif UserInputs==('Disk 2')
kg_2=input('Weight: \n');
r_2=input('Radius: \n');
elseif UserInputs==('Disk 3')
kg_3=input('Weight: \n');
r_3=input('Radius: \n');
end
switch UserInputs
case 'Disk 1'
figure(1)
plot(x1,y1)
case 'Disk 2'
figure(1)
plot(x2,y2)
case 'Disk 3'
figure(1)
plot(x3,y3)
end
x1=1
x2=2;
x3=3;
y1=(1./2).*kg_1.*r_1.^2
y2=(1./2).*kg_2.*r_2.^2
y3=(1./2).*kg_3.*r_3.^2
0 commentaires
Réponse acceptée
Voss
le 24 Mar 2022
Use strcmp() to compare character vectors; don't use == for that.
And if you're plotting one point at a time, you have to use a Marker so you can actually see the point on the axes. I've put markers ('o','s','d') <=> (circle, square, diamond) in the calls to plot().
And use hold on so that subsequent calls to plot() don't clear out what was already plotted.
The code below will take user-input for (and plot) one point. You can put the code inside a for loop or a while loop to make it take all three inputs, or just run it three times as it is.
disp('Please input a weight and radius for one of the disks.')
UserInputs=input('Please choose a disk (Disk 1, Disk 2, Disk 3):','s')
if strcmp(UserInputs,'Disk 1')
kg_1=input('Weight: \n');
r_1=input('Radius: \n');
elseif strcmp(UserInputs,'Disk 2')
kg_2=input('Weight: \n');
r_2=input('Radius: \n');
elseif strcmp(UserInputs,'Disk 3')
kg_3=input('Weight: \n');
r_3=input('Radius: \n');
end
switch UserInputs
case 'Disk 1'
figure(1)
hold on
x1=1;
y1=(1./2).*kg_1.*r_1.^2;
plot(x1,y1,'o')
case 'Disk 2'
figure(1)
hold on
x2=2;
y2=(1./2).*kg_2.*r_2.^2;
plot(x2,y2,'s')
case 'Disk 3'
figure(1)
hold on
x3=3;
y3=(1./2).*kg_3.*r_3.^2;
plot(x3,y3,'d')
end
2 commentaires
Voss
le 24 Mar 2022
Modifié(e) : Voss
le 24 Mar 2022
I assume this is in AppDesigner.
Give your DropDown a ValueChangedFcn called vcfDropDown (by clicking on the DropDown in Design View and selecting Callbacks in the Component Browser on the right).
Then in Code View, define vcfDropDown as follows (this assumes you have a UIAxes in your app called app.UIAxes - and that that's where you want to plot the points):
function vcfDropDown(app,evt)
disk_idx = find(strcmp(app.DropDown.Items,app.DropDown.Value));
kg = input('Weight: \n');
r = input('Radius: \n');
hold(app.UIAxes,'on');
markers = 'osd';
plot(app.UIAxes,disk_idx,(1./2).*kg.*r.^2,markers(disk_idx));
end
This is still taking user-input from the command-line, but I assume that ultimately you'll want to have UI components where the user would enter those things.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!