Umer Farooq
Umer Farooq

Reputation: 7486

Creating arrays in Matlab

I want to store user inputs in an array, but when a person enters a new number, the previous input gets replaced. How can I create such an array in Matlab such that I can store all inputs without replacement? I am a beginner so bear with me

Thanks

Upvotes: 1

Views: 1505

Answers (2)

yuk
yuk

Reputation: 19870

If you want a numeric matrix here is an example:

n = 2; %# number of rows
m = 3; %# number of columns
out = zeros(n,m); %# the output
k = 1; %# counter
while k <= n*m
    x = input('Enter a number or Enter to stop: ');
    if isempty(x)
        break
    else
        out(k)=x;
    end
    k=k+1;
end
disp(xx)

Upvotes: 0

Mike Fineli
Mike Fineli

Reputation: 66

You simply need to copy the contents of the input buffer into a data struct that won't be overwritten.

Cell arrays are good for that (see the userInputs variable below) . Without better knowledge of your code, I'm guessing the user input is stored in a variable named buffer. Here's how I would do it:

% a new buffer comes in
userInputs{iInput} = buffer;
iInput = iInput + 1;
% keep looking for more inputs

Good luck!

Upvotes: 1

Related Questions