Changing array size of user input data

4 views (last 30 days)
I am working on a script to take user input for a number, then create an array with 5 columns from 1 to said number. I am having trouble changing the array from a inputX1 matrix to an inputX5 could someone help me please. I tried to use the reshape function but since the input can be a nonmultiple of 5 the function doesn't work all the time. Here is the code
clear
% clc
name=input('Enter name:','s');
% Ask user to input name
number=input('Enter a whole number between 1 and 100:');
% Ask user for a whole number between 1 and 100
while number<1 || number>100 || round(number)~=number
% a while function that performs actions under if number doesn't meet
% specifications
display('Not a valid number please try again')
number=input('Enter a whole number between 1 and 100:')
end
% End while loop
array=[1:number];
% Create array from 1 to input number
entryposition=0;
% Set entry position to 0 for start of loop
disp('Thank you!')
disp(name)
disp(array')
  4 Comments
John Furman
John Furman on 29 Jan 2020
The idea is that any number between 1 and 100 is a valid input, unless the number isn't whole. However I want the final output for the array to be a vector with 5 columns and n amount of rows (depending on what the input is). So say 7 was the input. I would like the output to be:
1 2 3 4 5
6 7
I am not looking to round the number off to the nearest 5. Does that give a better idea of what I am looking for?
John Furman
John Furman on 29 Jan 2020
it doesn't have to necessarily be in a matrix but I am going to be exporting the data to an excel file afterwards and figured it would be easiest to format into a matrix before exporting the data. If there is an easier way that I just am not seeing I am open to suggestions. All I really need is to have it in a excel sheet in the end with 5 columns.

Sign in to comment.

Accepted Answer

Ridwan Alam
Ridwan Alam on 29 Jan 2020
Edited: Ridwan Alam on 29 Jan 2020
myCell = {}; k = 0;
for i = 1:5:number
k = k + 1;
myCell{k,1} = i:min(i+4,number);
end
writecell(myCell,fullfile(myFolder,'myFile.xls'))
p.s: this is not an "optimal" solution.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!