Storing x and y data calculated in loop as arrays to be used in the creation of a table.
11 次查看(过去 30 天)
显示 更早的评论
Hello. I am currently taking data during a loop (changing an outside variable M with each loop). This data is in the form of two arrays of length N. As of now my code plots these arrays and then goes on to the next loop, overwriting the arrays. I am wondering how I might store these values, to create a table something like this, adding columns x and y for each M(iteration). I would then try to export it to a csv/excel, but that's later I suppose lol.
I tried to create an empty array of zeros(N, 2*M) and then assign the x and y of each iteration to the i and i+1 (odds and even) respectively, but that wasn't fruitful. Do you folks have any other ideas as to what I might do?
0 个评论
采纳的回答
Walter Roberson
2023-4-6
M = table();
for K = 1 : 3
X = randi(9, 5, 1); Y = randi(9, 5, 1);
thisM = table(X, Y);
M = addvars(M, thisM, 'NewVariableNames', "M" + K);
end
M
M_to_write = splitvars(M);
writetable(M_to_write, 'YourFile.csv')
dbtype YourFile.csv
If you need the headers M1 M2 M3 in the file, then you have questions about what columns to write them into.
For text files you can write a header into a file using any of several text-writing functions, and then you can writetable() with 'writemode', 'append' . Or, you can split your table into cells and add a header cell and use writecell()
2 个评论
Walter Roberson
2023-4-6
The key to my code is that you can put a table() object inside a table() object, and if you do then it displays nicely. But unfortunately writetable() cannot handle writing the multiple levels of headers.
更多回答(1 个)
Oguz Kaan Hancioglu
2023-4-6
You can use table data object in matlab.
Best
x = 1:4';
y = 1:4';
M1 = table(x,y)
M2 = table(x,y)
M3 = table(x,y)
T = table(M1,M2,M3)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!