How does one store all the words from a loop into a variable?

for j = 1:length(wordsB)
PositionB = Shuffle2 (j); % Use number from the random array
% wordsB (PositionB); % Assign random position to words in text file
RandomWordsB = wordsB(PositionB);
% RandomWordsB = wordsB(PositionB); %Assign the obtained list to a varible to be displayed
disp (RandomWordsB) %display the random word list
end
RandomWords = [RandomWordsA; RandomWordsB]; %combining words from both arrays
disp (RandomWords) %display the random word lists

댓글 수: 2

It would have been easier for people to help you if you had attached the text files.
...or at least format the code using alt+enter.

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

 채택된 답변

madhan ravi
madhan ravi 2018년 12월 15일
You can store them as cells like below:
RandomWordsA=cell(1,numel(wordsA));
for i = 1:length(wordsA)
PositionA = Shuffle1 (i); % Use number from the random array
% wordsA (PositionA); % Assign random position to words in text file
RandomWordsA{i} = wordsA(PositionA); %Assign the obtained list to a varible to be displayed
disp (RandomWordsA) %display the random word list
end
celldisp(RandomWordsA)

댓글 수: 2

It didn't exactly work out but it did help me reach the right answer! Thank you!
Anytime :) , glad you found some way.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 12월 15일
RandomWordsA(i) = wordsA(PositionA); %Assign the obtained list to a varible to be displayed
and
RandomWordsB(j) = wordsB(PositionB);
We cannot tell what datatype you are working with, but I suspect that to get the 12 x 2 array that you want, you will need to change
RandomWords = [RandomWordsA; RandomWordsB]; %combining words from both arrays
to
RandomWords = [RandomWordsA(:), RandomWordsB(:)]; %combining words from both arrays

댓글 수: 1

Hmm that didn't quite work, I'm looking to somehow add to RandomWordB, every new element created by the loop

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 12월 14일

편집:

2018년 12월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by