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

2 次查看(过去 30 天)
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