Setting up and calculating using an array and ginput?
1 次查看(过去 30 天)
显示 更早的评论
Hi I'm trying to get my code to calculate the difference between x, y coordinates held in an array and that acquired from ginput.
So I've got two x, y ginputs that I want to calculate the difference between the first and second pair of values in my array called "ActualValues", so in this case I'd like to get the difference between ginput1 and 124, 93 and the difference between ginput2 and 109, 77.
How do I do this?
I'm using the following code:
clear clc close all
Map=imread('Map.Jpg'); imshow(Map); hold on;
ActualValues = [124 93; 109 77; 125 83; 117 75; 122 80]
uiwait(msgbox('Choose Point A')); [x,y] = ginput(1); hline = plot(x,y,'r+', 'MarkerSize', 50);
A = ginput(1);
uiwait(msgbox('Choose Point B')); delete(hline); [x,y] = ginput(2); plot(x,y,'r+', 'MarkerSize', 50); B = ginput(1);
disp ('Values:') disp(A); disp(B);
Would really appreciate some help, although I understand a minus operation should do the trick, I'm having difficulty referencing the ginputs and the array accordingly.
Many Thanks.
采纳的回答
YT
2017-12-14
编辑:YT
2017-12-14
Okay I did a little bit of adjustment to your code, but I think this is what you want to achieve
clear all;
close all;
Map = imread('Map.Jpg');
imshow(Map);
hold on;
uiwait(msgbox('Choose Point A'));
A = ginput(1);
hline = plot(A(1),A(2),'r+', 'MarkerSize', 50);
uiwait(msgbox('Choose Point B'));
delete(hline);
B = ginput(1);
plot(B(1),B(2),'r+', 'MarkerSize', 50);
disp(['Input 1; (x,y) = (' num2str(A(1)) ',' num2str(A(2)) ')'])
disp(['Input 2; (x,y) = (' num2str(B(1)) ',' num2str(B(2)) ')'])
diffBA = B-A; %difference B and A
disp(['Difference; (x,y) = (' num2str(diffBA(1)) ',' num2str(diffBA(2)) ')']);
I noticed that you didn't quite know how to display variables yet. I think you can figure it out yourself from the code above, but here's some clarification:
A(1) %x-coord. 1
A(2) %y-coord. 1
num2str(A(1)) %creates string from the numeric value for display purposes
disp(['...' here-your-string variable]) %use square brackets if you want to use variables in your disp()
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!