Matlab- storing iterations into a single matrix and file

In summary, the conversation was about a program that fits x- and y-data to a function and gives optimum parameters. The person wanted to perform a for loop to plot the change in those parameters over time. The suggested solution was to preallocate a matrix and concatenate the new data during each iteration of the loop, or to use a cell array and convert it to numeric and reshape it afterwards.
  • #1
Void123
141
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
  • #2
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.
 

FAQ: Matlab- storing iterations into a single matrix and file

1. How can I store iterations from a loop into a single matrix in Matlab?

To store iterations from a loop into a single matrix in Matlab, you can use the cat function. This function takes in a cell array of matrices and concatenates them along a specified dimension. In this case, you would specify the dimension as 1 to concatenate the matrices vertically. For example, if your loop generates a new matrix in each iteration, you can store them in a cell array and then use cat(1, cell_array{:}) to combine them into a single matrix.

2. How do I save the iterations from a loop into a file in Matlab?

To save the iterations from a loop into a file in Matlab, you can use the fprintf function. This function allows you to write data to a file with customizable formatting. You can use a loop to iterate through your data and use fprintf to write each iteration to the file. Alternatively, you can also use the dlmwrite function to save a matrix directly to a file.

3. Can I store iterations from a loop into a matrix and a file at the same time?

Yes, you can store iterations from a loop into a matrix and a file at the same time in Matlab. You can use the cat function to concatenate the matrices and then use fprintf or dlmwrite to save the matrix to a file. Alternatively, you can also use the diary function to record the output of your commands in a file.

4. How can I access and manipulate the iterations from a loop in Matlab?

To access and manipulate the iterations from a loop in Matlab, you can store them in a matrix or cell array. This will allow you to use Matlab's indexing and matrix manipulation functions to modify the data. You can also use conditional statements within the loop to perform specific actions on each iteration.

5. Is it possible to store iterations from a loop into a single matrix and file in a specific format?

Yes, it is possible to store iterations from a loop into a single matrix and file in a specific format in Matlab. You can use the fprintf function with appropriate formatting options to write the data to a file in a specific format. You can also use the dlmwrite function with a delimiter option to save the data in a specific format.

Similar threads

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