Hi all,
I have a function called details, where the user enters his name, and the details are stored in the array, say "name". Each time a name is entered it should get appended to the array.
At any point, the user can cancel entering the details and do other computations. Then again the user can start entering the details. In this case, it should get appended to the "name" array that is already existing.
At the beginning of the session, the array "name" should be empty.

 채택된 답변

Yatin
Yatin 2013년 10월 16일
편집: Yatin 2013년 10월 16일

5 개 추천

Hi,
A possible solution to your problem could be something like this. Set the array as empty initially(beginning of the session).
nameArray = {};
You can then append the entries in the array as follows:
nameArray = [nameArray, 'Name you want to append'];
This uses the concept of cell array. You might want to refer to the documentation on cell arrays for more information. http://www.mathworks.com/help/matlab/cell-arrays.html

댓글 수: 6

Cooper Harris
Cooper Harris 2018년 10월 19일
편집: Cooper Harris 2018년 10월 19일
this is an awful answer. does not address how to iteratively update an array ("list" in python). this simply redefines the array and erases past values. the link you posted was helpful though!
James Tursa
James Tursa 2018년 10월 19일
"... this simply redefines the array and erases past values ..."
Really? Did you try it?
Steven Lord
Steven Lord 2018년 10월 19일
If you run both commands over and over again, nameArray will keep only the last name entered.
If you run the first command once to initialize the variable then run only the second line repeatedly to append to the cell array, it will do what the original poster asked.
Bill Tubbs
Bill Tubbs 2020년 3월 22일
Sad that this is the only solution since it's not efficient (reallocates memory each iteration) but what can you do... Thanks!
bla bla
bla bla 2020년 4월 13일
@Bill Tubbs maybe try nameArray(end+1) = {'name to be appended'}; should be a more efficient way of doing it
testFactor = 194225602
testStart = 3928
tic
mytest = [testStart];
for i = 1:100000
mytest = [mytest, testStart];
end
toc
tic
mytest = [testStart];
for i = 1:100000
mytest(end+1) = testStart;
end
toc
The result is :
2.024820
0.011121
list = [list, appendedNumber] is ridiculous.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Vinaya Babu Yerneni
Vinaya Babu Yerneni 2020년 10월 16일

0 개 추천

Firstly you need to clarify whether you need to append strings or integer or string and integer
for only strings
name=input('What is your name? ', 's');
age=input('What is your age? ',"s");
hometown=input('What is your hometown? ', 's');
sport=input('What is your favorite sport? ', 's');
Temptex2=append(name,age,hometown,sport)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2013년 9월 18일

댓글:

2021년 6월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by