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.

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)

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

I'm trying to get numeric values. For example, if the loop ran twice the code should spit out, A,B,C values for both times the loop ran.
Eg.
3 4 5
2 4 5
My main issue is that the loop will only display the last values that were inputted. I need the info in an array
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.

This question is closed.

Products

Tags

Asked:

on 29 Jun 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!