Storing x and y data calculated in loop as arrays to be used in the creation of a table.

조회 수: 13 (최근 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?

채택된 답변

Walter Roberson
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 = 5×3 table
M1 M2 M3 X Y X Y X Y ______ ______ ______ 8 6 5 4 4 4 9 1 6 2 1 9 6 1 1 9 7 5 6 9 9 7 6 4 6 5 3 6 2 9
M_to_write = splitvars(M);
writetable(M_to_write, 'YourFile.csv')
dbtype YourFile.csv
1 M1_X,M1_Y,M2_X,M2_Y,M3_X,M3_Y 2 8,6,5,4,4,4 3 9,1,6,2,1,9 4 6,1,1,9,7,5 5 6,9,9,7,6,4 6 6,5,3,6,2,9
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
Dametre
Dametre 2023년 4월 6일
That makes so much sense. I'm rather new to MATLAB (Python experience), So seeing this laid out it looks like you're using the iterator to add a unique dataset of x,y, which doesn't cause an overwrite/duplicate name. I really appreciate your time, It is very helpful for me to see the code written out in such a way. I don't think I need the headers in the file, I may end up attempting to do so to help me learn more, but I believe I can just index numerically in this application :)
Thank you so much!
Walter Roberson
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
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)
M1 = 1×2 table
x y ________________ ________________ 1 2 3 4 1 2 3 4
M2 = table(x,y)
M2 = 1×2 table
x y ________________ ________________ 1 2 3 4 1 2 3 4
M3 = table(x,y)
M3 = 1×2 table
x y ________________ ________________ 1 2 3 4 1 2 3 4
T = table(M1,M2,M3)
T = 1×3 table
M1 M2 M3 x y x y x y ____________________________________ ____________________________________ ____________________________________ 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2013b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by