Plot figure for certain elements of readtable output

조회 수: 1 (최근 30일)
mark savage
mark savage 2022년 7월 20일
편집: Abderrahim. B 2022년 7월 20일
Hi,
I have a readtable output, T, = (for example):
Longitude | Latitude
1 34
1 56
1 57
4 72
4 44
4 124
I want to plot a graph of Long vs Lat but only for Long = 1 (so the coordinates of this graph will be: (1, 34), (1, 56), (1, 57).
I tried this code but it just gives me a matlab crash report, I assume cause it requires a lot of iterations for my actual readtable output. Also, I just end up getting a bunch of empty figures:
for long_ind = numel(T{1,1}.Longitude)
while T{1,1}.Longitude(long_ind,1)==1
figure
plot(T{1,1}.Longitude(long_ind,1),T{1,1}.Latitude(long_ind,1))
hold on
end
end
Thank you

답변 (2개)

Jonas
Jonas 2022년 7월 20일
you can use directly index the variables of the table:
tbl=table();
tbl.long=[1 2 3 4 4 4];
tbl.lat=[34 56 57 72 44 124];
myCond=tbl.long<=3; % change as you want
plot(tbl.long(myCond),tbl.lat(myCond));

Abderrahim. B
Abderrahim. B 2022년 7월 20일
편집: Abderrahim. B 2022년 7월 20일
Hi!
As per my understading of your question, the code below should work:
%% Create table with 2 variables (Log and Lat)
data = table([1 1 1 4 4 4].', [34 56 57 72 44 124].' , 'VariableNames',["Long", "Lat"]) ;
long = data.Long;
lat = data.Lat ;
%% Plot Long vs Lat (Long = 1)
figure
hold on
plot([lat(long == 1) lat(long == 1)], [long(long ==1) long(long == 1)],'r' )
Bonus: The below lines of code are just to make plot more readable
% Add coordinates as "points" using satter func
scatter([lat(long == 1) lat(long == 1)], [long(long == 1) long(long == 1)], 'filled')
offset = 0.1 ; % just to not overlap point with text
% Plot annotation
text( lat(long == 1) ,long(long == 1) + offset, strcat( num2str(long(long == 1)), ',' ,num2str(lat(long == 1)) ))
xlabel Latitude
ylabel Longitude
title ("Long vs Lat (Long = 1)")
Hope this help

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by