How to load a variable and export and edited version of it into a matrix
1 view (last 30 days)
Show older comments
Basically what I have is this.
function ecef2lla = LLA (lats, longs, hts)
f = 1/298.257224;
e = 2*f - f.^2;
a = 6378137;
N = a/(1-e.^(2)*sin(lats)).^(1/2)
x = (N+hts)*cos(lats)*cos(longs);
y = (N+hts)*cos(lats)*sin(longs);
z = (N*(1-e.^2)+hts)*sin(lats);
ecef2lla = [x;y;z]
end
%%% new function, this is the actual code I'm trying to run
load ('Matrix1')
lats = Matrix1(1,1)
longs = Matrix1(1,2)
hts = Matrix1(1,3)
Matrix2 = zeros (207,3)
for L = 1:207
Matrix2(L,:) = LLA (lats(L), longs(L), hts(L))
end
I'm trying to load Matrix1 into the function and export the x,y,z edit into Matrix2. It's a 207,3 matrix.
0 Comments
Accepted Answer
Guillaume
on 21 Sep 2019
So, what doesn't work with your current code? It's difficult to tell you what to change if we don't know what the problem is.
One obvious problem is that you never save anything, so the export certainly doesn't happen. Since you know about load presumably you also know about save.
You haven't said what sort of file you want to create. a mat file, a text file, an excel file, something else? For that matter, you haven't told us what the input file is. It looks like it's a mat file but it could also be a text file with no extension.
One other obvious problem is that your lats, longs and hts are only one element of Matrix1, the values from the first row. So lats(L), longs(L) and hts(L) are all going to be invalid operations for any L greater than 1. Possibly you meant:
lats = Matrix1(:, 1);
longs = Matrix1(:, 2);
hts = Matrix(:, 3);
Since your data is organised by row, it would make a lot more sense if you concatenated x, y, and z horizontally instead of vertically in your LLA function:
ecef2lla = [x, y, z]; %horizontal concatenation
This would be particulalry helpful if you modified the LLA function to work with column vectors inputs instead of just scalar. For that you'd have to make sure to use dotted operators for the operations that involve vector. Ironically, most of the ones you've dotted only operate on scalar so the dot wasn't needed.
function ecef2lla = LLA (lats, longs, hts) %works with lats, longs and hts column vectors
f = 1/298.257224;
e = 2*f - f^2; %no need for .^ since f is scalar
a = 6378137;
N = a./sqrt(1-e^2*sin(lats)); %division by a vector requires ./, replaced .^(1/2) by sqrt
x = (N+hts).*cos(lats).*cos(longs); %vector multiplication .*
y = (N+hts).*cos(lats).*sin(longs);
z = (N*(1-e^2)+hts).*sin(lats); %e is scalar no need for .^, vector multiplication so .*
ecef2lla = [x, y, z]
end
with that you no longer need a loop in your other function:
Matrix2 = LLA(Matrix1(1, :), Matrix1(2, :), Matrix1(3, :));
With this you also no longer need to preallocate Matrix2 but if you had to, note that it's never a good idea to hardcode the size. If you used a files with more or less than 207 rows your code would no longer work. Much better to ask matlab how many rows your data has and use that:
Matrix2 = zeros(size(Matrix1, 1), 3); %if preallocation was required.
2 Comments
Guillaume
on 22 Sep 2019
I'm not entirely sure what code you now have. If it's exactly what you've posted where you've grouped both functions inside just one then yes, it's not going to work.
I assume that your Matrix1.mat contains a Matrix1 variable. Isn't that supposed to be your lats, longs and hts inputs? WIth your new code you're first using some unknown lats, longs, and hts (possibly leftover from previous calculations) to calculate x, y, z, then for some reason multiply these with the columns of your Matrix1 to create brand new lats, longs and hts. That's what you're later saving into Matrix2.mat. You then create a Matrix2 using the 3 columns of Matrix1 (thus completely ignoring what you've done so far) and never do anything with Matrix2.
Finally, as I (tried to) explain,
Matrix2 = zeros(size(Matrix1, 1), 3);
would be the correct way to preallocate Matrix2 if you were looping over the rows. As you're no longer looping, this is no longer needed and is actually counterproductive. You're creating a matrix of 0 which is going to be deleted and replaced on the next line.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!