Filling a matrix row by row and saving it to a .mat file
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Stephen
on 30 Jun 2014
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 Comments
Stephen
on 1 Jul 2014
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
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!