Loop Code Through Each Row of Excel Spreadsheet and Save Result Matrix
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Cris LaPierre
il 27 Feb 2023
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")
Più risposte (1)
M.B
il 27 Feb 2023
Modificato: M.B
il 8 Ago 2023
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");
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!