MATLAB Matlab- storing iterations into a single matrix and file

AI Thread Summary
The discussion revolves around a programming challenge involving the collection and plotting of parameter estimates from a fitting function over multiple iterations. The user seeks to implement a for loop that captures the changes in fitted parameters during a real-life experiment, specifically at one-second intervals. A suggested solution involves preallocating a matrix to store the estimates, allowing for efficient data collection during each iteration. An alternative approach mentioned is using a cell array to store each iteration's parameters, which can later be converted to a numeric format for plotting. The conversation emphasizes that performance tuning may not be necessary for a small number of iterations, such as five.
Void123
Messages
138
Reaction score
0
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.
 
Physics news on Phys.org
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.
 

Similar threads

Replies
1
Views
2K
Replies
4
Views
1K
Replies
32
Views
4K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
12
Views
4K
Back
Top