How to save Data Individually

1 view (last 30 days)
C PRASAD
C PRASAD on 9 Feb 2022
Commented: C PRASAD on 15 Feb 2022
I Have a Data
like 2000*5 i.e 2000 rows with 5 colomns.
I would like to save this the data in to five varaible like x1,x2,x3,x4 and x5 where x1 consisting of 1st coloms of x.
The MATLAB code must be any loop function.How to Write and save 2000*1 data of 5 different variables using a loop statement with MATLAB
  2 Comments
Stephen23
Stephen23 on 9 Feb 2022
Edited: Stephen23 on 9 Feb 2022
"I would like to save this the data in to five varaible like x1,x2,x3,x4 and x5"
That is very unikely to be a good approach:
You can simply access the data directly from the matrix using basic indexing, or split the matrix into a cell array:
C = num2cell(M,1)
C PRASAD
C PRASAD on 15 Feb 2022
Thanks for the answe

Sign in to comment.

Answers (1)

Addy
Addy on 9 Feb 2022
For the reason @Stephen mentioned. Only way to create variables in a loop is using eval function.
It is bad to use eval to create variables in loop.
So, you can do it manually like
x1 = x(:,1);
x2 = x(:,2);
....
Or in a loop to store in a cell
for i=1:size(x,2)
x_cell{:,i} = x(:,i);
end
Or just use num2cell
x_cell = num2cell(x,1);
  3 Comments
Stephen23
Stephen23 on 11 Feb 2022
Edited: Stephen23 on 11 Feb 2022
"Now I would like to give name to each column..."
Then use a table:
M = rand(8000,5);
T = array2table(M, 'VariableNames',{'a','b','c','hello','world'})
T = 8000×5 table
a b c hello world _________ ________ ________ ________ ________ 0.29025 0.1223 0.49279 0.34924 0.9614 0.56472 0.71979 0.79971 0.28213 0.48654 0.049005 0.90739 0.38714 0.56751 0.56329 0.5461 0.49098 0.9208 0.47405 0.2342 0.40111 0.28172 0.8404 0.39891 0.53901 0.13418 0.56801 0.2039 0.019276 0.52889 0.52612 0.74784 0.96346 0.43849 0.67462 0.018373 0.070646 0.29265 0.7457 0.52281 0.69947 0.29329 0.65923 0.4547 0.014092 0.31798 0.27716 0.97755 0.61423 0.67483 0.0048459 0.46726 0.035125 0.24189 0.90291 0.67208 0.825 0.68594 0.98525 0.2302 0.85427 0.79711 0.87272 0.98439 0.29113 0.85162 0.9343 0.22138 0.10056 0.31589 0.077557 0.95805 0.21503 0.80634 0.069713 0.43137 0.78357 0.16262 0.80901 0.37363
C PRASAD
C PRASAD on 15 Feb 2022
Thanks for the answer

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!