Filling a matrix row by row and saving it to a .mat file

조회 수: 2 (최근 30일)
Victor
Victor 2014년 6월 30일
댓글: Stephen 2014년 7월 1일
I have a function with input arguments which everytime it is called, it will fill a matrix according to those input arguments. For example: func(a,b,c,d,e)
Everytime the function is called, it will keep on filling a matrix until 'a' is reached:
for example if the user inputs func(3,1,2,3,4), the output will be:
1 1 2 3 4 2 1 2 3 4 3 1 2 3 4
This will be saved to a matrix first, then saved into a .mat file. However, every time i call the function with different inputs, the previous values are overwritten with the new input values from the function call. I have used the -append but that stil overwrites the previous values. Is there a way so that everytime i do a function call, the the new inputs will append to the previous values in the matrix instead of overwriting them? For example:
func(3,1,2,3,4)
1 1 2 3 4 2 1 2 3 4 3 1 2 3 4
func(4,1,2,3,3)
1 1 2 3 4 2 1 2 3 4 3 1 2 3 4 4 1 2 3 3 5 1 2 3 3 6 1 2 3 3 7 1 2 3 3
As you can see i also want to make the index continue counting from the previous index everytime i call the function. Thank you.

답변 (1개)

Stephen
Stephen 2014년 6월 30일
I think what you're after is horizontal concatenation.
One way:
a = 1; %a has to be defined first
%then inside of some loop, maybe?
a = [a function(a,b,c,d,e)];
%right before you're done with it
a = a(2:end); %gets rid of the junk assigned when a = 1 on the first line
  댓글 수: 2
Victor
Victor 2014년 6월 30일
Is there an alternative way? Essentially I want to call the function a number of times and fill the matrix horizontally. Then at the end, I want to save the entire matrix as a variable into a .mat file. There wont be just one variable inside the .mat file, but i would like to have many different matrices in the .mat file as well.
Stephen
Stephen 2014년 7월 1일
The variable at the end of the last statement "a = a(2:end)" should be what you want for that variable. Saving more than one variable to the mat file is independent of how they are actually constructed and can be accomplished as:
save('filename.mat','a','variable2','variable3'); %and so on

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

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by