created a function that inputs coordinate points like (1,2) in a loop for n number of points and keeps the x and y in seperate arrays. problem is it is only taking the first numbers entered... any clues?
    6 ビュー (過去 30 日間)
  
       古いコメントを表示
    
function[x,y] = readCoordinates(n)
    x = zeros(n,1);
    y = zeros(n,1);
   for i = 1:n 
        enterString = input('Please enter coordinates within parentheses seperated by a comma.','s');
        components = sscanf(enterString,'%c%d%c%d%c');
        x(i) = components(2);
        y(i) = components(4);
   end
end
0 件のコメント
回答 (1 件)
  John Chilleri
      
 2017 年 4 月 28 日
        Hello,
Your code will work with one simple change:
function[x,y] = readCoordinates(n)
   x = zeros(n,1);
   y = zeros(n,1);
   for i = 1:n 
        enterString = input('Please enter coordinates within parentheses seperated by a comma.','s');
        components = sscanf(enterString,'%c%f%c%f%c');
        x(i) = components(2);      % CHANGE ^   ^ from d to f
        y(i) = components(4);
   end
end
This will search for floating point numbers which will solve the problem.
You might also consider changing,
'%c%f%c%f%c' to '(%f,%f)'
which would require changing,
x(i) = components(1);
y(i) = components(2);
but this is more of a stylistic preference, and I haven't fully considered the repercussions of either.
Hope this helps!
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

