Creating individual coordinate variables from excel

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 Comments

"What I would like: Cooridnate 1 ... Cooridnate 2... There is also an unknown number of coordinates to consider."
Creating/accessing numbered variable names like that is one way that beginners force themselves into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
The much simpler and very efficient approach is to use indexing into your matrix, just like MATLAB is designed for.
How would I create an index for undefined sized matrix? I will always have three columns but the amount of rows is unknown
"How would I create an index for undefined sized matrix?"
x = num(:,1);
% ^ You already have here. This refers to any number of rows.
You can access any specific row very easily and effiiciently using indexing, e.g. you can trivially loop over all of the rows:
N = size(p,1); % number of rows
for k = 1:N
row = p(k,:)
% do whatever with that row
end
Indexing is how MATLAB works, it is simple and very efficient (unlike what you are trying to do).
Using MATLAB without using indexing is like cutting off your foot before running a race.
"What I have:"
Looks more like a table to me. Did you really use XLSREAD and get 'X', 'Y', and 'Z' characters from its 1st output (which contains only numeric values) ? Or is your actual code something that you are not showing us?
How would I store each row of the index in order to do further calculations with the row? I'm trying to extract a row in order to do further calculations with the data.
Each row will be used for a seperate calculation. The first column is the X coordiante, the second is the Y, and the third is the Z. Each row represents a new cooridnate point. I am trying to read in a file of coordinate points and need to multiply each row, i.e. each cooridnate point, by a different transformation parameter and do several other calculations with the individual rows. This is going to be a generic code so that any excel file containing (X,Y,Z) coordiantes can be used.
"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
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)

Sign in to comment.

Answers (1)

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!

Asked:

on 19 Jan 2022

Answered:

on 25 Jan 2022

Community Treasure Hunt

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

Start Hunting!