Creating individual coordinate variables from excel
7 次查看(过去 30 天)
显示 更早的评论
I am trying to write a code that inputs coordinate data from excel and stores each row of x,y,z coordinates into its own variable.
So far I have this which brings in all my data and stores into a matrix containing all three coordinate points.
[num,txt,raw] = xlsread('Excel.xlsx');
x = num(:,1);
y = num(:,2);
z = num(:,3);
p = [x, y, z];
I am unsure of how to extract each row in order to have each set of coordinate points be stored in its own x,y,z variable.
What I have:
p =
1.0e+06 *
X Y Z
1.744644127200000 -4.014801833900000 4.623258965200001
1.671483114500000 -3.987389898500000 4.673416349999999
What I would like:
Cooridnate 1 = 1.744644127200000 -4.014801833900000 4.623258965200001
Cooridnate 2 = 1.671483114500000 -3.987389898500000 4.673416349999999
There is also an unknown number of coordinates to consider.
Thank you very much!
6 个评论
Stephen23
2022-1-19
编辑:Stephen23
2022-1-19
"How would I store each row of the index in order to do further calculations with the row?"
Look at the code in my last comment: do you see in the FOR loop where I wrote "do whatever with that row": that is the location where you write your code that does Whatever You Want with that row of data, which I named "row" for you. To make it clear and easy for you to understand, that it will be just one row of data, just as you ask for. One row. Just one. Which is what you asked for, so I showed you how. You really can do Whatever You Want with it: email it to your friends, plot it, or perform some calculation. On just one row. One. Named "row".
And then, because this is what FOR loops do, it will apply your Whatever You Want on the next row. Automatically.
One. row. at. a. time.
"I'm trying to extract a row in order to do further calculations with the data."
The code in my last comment shows how to extract one row using very simple indexing.
I strongly recommend doing these tutorials, which introduce how indexing works: https://www.mathworks.com/help/matlab/getting-started-with-matlab.html
Cris LaPierre
2022-1-19
- Ch 4 - vectors and matrices
- Ch 5 - Indexing into and modifying arrays and matrices
- Ch 6 - array calculations
- Ch 2 - programming (section 2 covers for loops)
回答(1 个)
Sailesh Kalyanapu
2022-1-25
Hi,
You can extract the individual rows using a for loop and then use a cell array to store the rows individually. You can index the cell array to obtain desired rows later.
Please use the following code snippet for reference:
[num,txt,raw] = xlsread('Excel.xlsx');
[rowL,columnL] = size(num);
Coords_cellarray = {};
for i = 1:rowL
Coords_cellarray(i,:) = {num(i,:)} ;
end
%% To access the ith row extracted, you can use ith_row = Coords_cellarray{i};
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!