Creating individual coordinate variables from excel

조회 수: 9 (최근 30일)
Janica Johnson
Janica Johnson 2022년 1월 19일
답변: Sailesh Kalyanapu 2022년 1월 25일
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
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
Cris LaPierre 2022년 1월 19일
I'd also suggest the following chapters in MATLAB Onramp.
  • 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
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};
Use of readtable, readmatrix, or readcell is recommended instead to read the xls file.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by