Can't produce multiple Circles
2 次查看(过去 30 天)
显示 更早的评论
Hi!
I am currently trying to create multiple circles on App Designer. The idea of this project is that the user is meant to insert the x,y and radius inputs and it should be plotted on the graph/axes.
I have tried to write some code, but it just wouldn't plot for some reason. This is from the last question I asked, but I am posting again due to urgency and need to finish this project ASAP.
Would appreciate all the assistance I can get!! Thank you!!
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter values';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
T_L = 'Circle'
dims = [1 40]
caca = 2*pi;
ang = linspace(0,caca);
r = str2double(caUserInput{1});
C = str2double(caUserInput{2});
xp = C(1)*cos(ang);
yp = C(2)*sin(ang);
plot(xp,yp,'r-')
0 个评论
回答(1 个)
Image Analyst
2022-12-18
编辑:Image Analyst
2022-12-18
It's simple to adapt. Not sure you tried to adapt it since it's the same snippet I gave you before. Anyway, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Ask user for two floating point numbers.
defaultValue = {'5', '2', '3'};
titleBar = 'Enter values';
userPrompt = {'Enter x : ', 'Enter y: ', 'Enter Radius'};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Convert to floating point from string.
x = str2double(caUserInput{1})
y = str2double(caUserInput{2})
radius = str2double(caUserInput{3})
% Check usersValue1 for validity.
if isnan(x)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
x = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', x);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(y)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
y = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', y);
uiwait(warndlg(message));
end
% Do the same for usersValue3
% Check usersValue3 for validity.
if isnan(radius)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
radius = str2double(defaultValue{3});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', y);
uiwait(warndlg(message));
end
angles = linspace(0, 2 * pi, 400);
xCircle = x + radius*cos(angles);
yCircle = y + radius*sin(angles);
plot(xCircle, yCircle, 'r-', 'LineWidth', 2)
grid on;
axis equal
Anyway, if you're using app designer you wouldn't use inputdlg() - you'd place edit text boxes on the window and get the numbers from there, like
x = str2double(app.edtX.Text);
y = str2double(app.edtY.Text);
radius = str2double(app.edtRadius.Text);
15 个评论
Image Analyst
2022-12-19
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!