Loop Code Through Each Row of Excel Spreadsheet and Save Result Matrix
3 次查看(过去 30 天)
显示 更早的评论
I haven't been on MATLAB for a while. I suppose I am very rusty. But I have a spreadsheet, and there are two columns of interest: one is labeled x_coord and the other is labeled y_coord. For each row of both columns, I am converting the x and y coordinate values to longitude and latitude degree values. Then, I am writing the results in a matrix. However, it is just not working. I am looking to have a pair of latitude and longitude values for each row, based on the x and y cooredinates. I'd greatly appreciate any help.
N=[];
for K = 1:172
data = readtable('zipcode_information2.csv', 'PreserveVariableNames',true)
X = data(:,ismember(data.Properties.VariableNames, {'x_coord'}))
Y = data(:,ismember(data.Properties.VariableNames, {'y_coord'}))
info = shapeinfo('ZIP_CODE_040114.shp');
proj = info.CoordinateReferenceSystem;
[lat,lon] = projinv(proj,X{i,1},Y{i,1});
outdata = [lat,lon]
N = [N; outdata]
end
writematrix(N, 'LongLat.csv',"WriteMode","append")
0 个评论
采纳的回答
Cris LaPierre
2023-2-27
You don't need a loop. Perform the calculation on the entire column at once.
data = readtable('zipcode_information2.csv', 'PreserveVariableNames',true)
info = shapeinfo('ZIP_CODE_040114.shp');
proj = info.CoordinateReferenceSystem;
[lat,lon] = projinv(proj,data.x_coord,data.y_coord);
N = [lat,lon]
writematrix(N, 'LongLat.csv',"WriteMode","append")
更多回答(1 个)
M.B
2023-2-27
编辑:M.B
2023-8-8
The issue is with your indexing. You declare K as the index and use i inside the loop.
I agree with Cris, you don't need a loop. The function "projinv" takes vectors as inputs. If you insist on using a loop, you can try the following:
% place all fixed parameters outside the loop
N=nan(172, 2);
data = readtable('zipcode_information2.csv', 'PreserveVariableNames',true);
X = data(:,ismember(data.Properties.VariableNames, {'x_coord'}));
Y = data(:,ismember(data.Properties.VariableNames, {'y_coord'}));
info = shapeinfo('ZIP_CODE_040114.shp');
proj = info.CoordinateReferenceSystem;
for K = 1:172
[lat,lon] = projinv(proj,X(K),Y(K));
outdata = [lat,lon];
N(K, :) = outdata;
end
writematrix(N, 'LongLat.csv',"WriteMode","append");
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!