Making a user input a matrix
Show older comments
I'm trying to create a function in where a user can input 1 to have an excel file of two lines of data read as (x,y) coordinates. But also that the user can select 2 so they can enter their own (x,y) coordinates but in comma-separated pairs. Like pairs in 2 rows. Whenever I try to enter values for z I only get an empty matrix and idk how to actually fix that. Would I need to do a loop or another function like creating a cell array? (After the matrix is created I'm also hoping to just read each row and attach them to either x and y).
if true
% code
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [x,y] = BB_GrabInputData(z)
%Creates an output of data depending on what the user inputs for vectors
% User must select either file or manual data points in an even-numbered
% vector
A=0;
while A == 0
User_DataSelect=input('<1>Read in a data file or <2>Enter data points by hand?' );
if User_DataSelect==1
z = xlsread(strinput('Enter in your file name'));
x = z(:,1);
y = z(:,2);
A= A + 1;
elseif User_DataSelect==2
disp('Enter in your data points in comma-seperated pairs.\n Press <Enter> without typing any numbers when you are done entering the data.\n')
z = input('');
z = z(end) + z
A=A+1;
else
disp('Error: Invlalid User Input') %Detects when an invalid selection occurs and the loop repeats
A=0;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 Comments
Brevon Baker
on 27 Feb 2018
Answers (1)
x = [];
y = [];
pmt = 'Enter comma-separated pairs. Press <Enter> with no data to exit. ';
while true
str = input(pmt,'s');
if numel(str)
tmp = sscanf(str,'%f,%f');
x(end+1) = tmp(1);
y(end+1) = tmp(2);
else
break
end
end
2 Comments
Brevon Baker
on 27 Feb 2018
Stephen23
on 27 Feb 2018
@Brevon Baker: I fixed the start conditions, it works now:
Enter comma-separated pairs. Press <Enter> with no data to exit. 1,2
Enter comma-separated pairs. Press <Enter> with no data to exit. 3,4
Enter comma-separated pairs. Press <Enter> with no data to exit. 5,6
Enter comma-separated pairs. Press <Enter> with no data to exit.
>> x
x =
1 3 5
>> y
y =
2 4 6
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!