Filling a matrix row by row and saving it to a .mat file
1 回表示 (過去 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.
0 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!