Multiple inputs for one graph
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Voss
2022년 3월 24일
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
Voss
2022년 3월 24일
편집: Voss
2022년 3월 24일
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!