Matlab- storing iterations into a single matrix and file

  • MATLAB
  • Thread starter Void123
  • Start date
  • #1
141
0

Main Question or Discussion Point

Hi guys:

I have a program which is supposed to fit x- and y-data to a certain function and give me the optimum parameters. This is no problem. What I want to do though is to perform a for loop that allows me to plot the CHANGE in those parameters over time (as I conduct a real life experiment). The code is something like this (an example):

for i=1:5
pause(1)
execute_function;


[estimates, model] = fitcurvedemo(xdata, ydata)

end

The output will give me five iterations of the fitted parameters (which I have a separately written program for which works), but I want to collect them into a single column and plot them against the time (i), which in this case is 1 second intervals.

I hope this is clear.

Thanks.
 

Answers and Replies

  • #2
kreil
Insights Author
Gold Member
668
67
You could preallocate a matrix, then just concatenate the new data into it during each iteration of the loop.

Code:
A = [];
for i=1:5
    pause(1)
    execute_function;
    [estimates, model] = fitcurvedemo(xdata, ydata)
    A = [A; estimates];
end
If you're only doing this 5 times then it probably isn't necessary to tune it for performance. Another way to accomplish this would be to use a cell array where the parameters from each iteration get their own cell, then afterward you convert it to numeric and reshape it.
 

Related Threads on Matlab- storing iterations into a single matrix and file

Replies
6
Views
2K
Replies
1
Views
5K
Replies
1
Views
2K
Replies
3
Views
47K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
4
Views
7K
Replies
5
Views
926
Replies
4
Views
2K
Replies
3
Views
22K
Replies
1
Views
1K
Top