Plot figure for certain elements of readtable output

1 次查看(过去 30 天)
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