Info

This question is closed. Reopen it to edit or answer.

Trying to Save all my inputted data from my for loop. I need to save the the Values of A,B and C into one matrix.

1 view (last 30 days)
Components = input('Enter Number of Liquid Components:' )
x = Components;
for r = 1:x
A = input('Enter A:');
B = input('Enter B:');
C = input('Enter C:');
end

Answers (1)

Geoff Hayes
Geoff Hayes on 30 Jun 2015
Sayyid - if you are expecting the user to enter numeric data, then you could just use an array to store this information. Try something like the following
numComponents = input('Enter Number of Liquid Components:' )
componentData = zeros(numComponents,3);
for r = 1:numComponents
componentData(r,1) = str2double(input('Enter A:','s'));
componentData(r,2) = str2double(input('Enter B:','s'));
componentData(r,3) = str2double(input('Enter C:','s'));
end
Note how we pre-size the componentData matrix and then convert each input string to a double using str2double. See input for why the 's' input is used.
However, if you are expecting the user to enter numeric and/or non-numeric data, then you could use a cell array instead
numComponents = input('Enter Number of Liquid Components:' )
componentData = cell(numComponents,3);
for r = 1:numComponents
componentData{r,1} = input('Enter A:','s');
componentData{r,2} = input('Enter B:','s');
componentData{r,3} = input('Enter C:','s');
end
Try either of the above and see what happens!
  3 Comments
Sayyid Ferouz
Sayyid Ferouz on 30 Jun 2015
My main issue is that the loop will only display the last values that were inputted. I need the info in an array
Geoff Hayes
Geoff Hayes on 1 Jul 2015
Sayyid - use the first example that I have posted above and that will allow you to save the numeric data (from each iteration of the loop) to an array.

Tags

Products

Community Treasure Hunt

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

Start Hunting!