Matlab- storing iterations into a single matrix and file

  • Context: MATLAB 
  • Thread starter Thread starter Void123
  • Start date Start date
  • Tags Tags
    File Matlab Matrix
Click For Summary
SUMMARY

The discussion focuses on storing multiple iterations of fitted parameters from a MATLAB function into a single matrix for plotting against time. Users are advised to preallocate a matrix and concatenate new data during each iteration of a loop. The provided code example demonstrates how to collect estimates from the 'fitcurvedemo' function into a matrix 'A'. Additionally, an alternative method using a cell array is suggested for organizing parameters from each iteration before conversion to numeric format.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of the 'fitcurvedemo' function for curve fitting
  • Knowledge of matrix operations in MATLAB
  • Basic concepts of loops and data structures in programming
NEXT STEPS
  • Explore MATLAB matrix preallocation techniques for performance optimization
  • Learn about MATLAB cell arrays and their applications in data storage
  • Investigate advanced plotting techniques in MATLAB for visualizing parameter changes
  • Review MATLAB documentation on the 'fitcurvedemo' function for deeper insights
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers conducting experiments that require iterative data collection and visualization of parameter changes over time.

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 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 12 ·
Replies
12
Views
4K