How to conditionally assign characters for a new column in a table
Show older comments
Hi,
Let's say that I have a simple table like so:
lat = [49, 50, 51, 52];
lat = lat'
lon = [-99, -100, -101, -102];
lon = lon'
T = table (lat, lon)
Now let's say that I wanted to add a station name based on the latitude in a new column 'station'. I could do like the code below but I am limited to using a single character as the station name (A, B, C or D) otherwise I get the following error: Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-9. Also, if I have a large number of stations I am going to have to add 2 lines for every stations which is clumsy. So. Is there a better way to do this (by better I mean a way that would diminish the number of lines required and allow multiple characters in the 'station' column) ?
mask = T.lat > 48.5 & T.lat < 49.5;
T.STATION(mask) = 'A';
mask = T.lat > 49.5 & T.lat < 50.5;
T.STATION(mask) = 'B';
mask = T.lat > 50.5 & T.lat < 51.5;
T.STATION(mask) = 'C';
mask = T.lat > 51.5 & T.lat < 52.5;
T.STATION(mask) = 'D';
1 Comment
Walter Roberson
on 31 Jul 2019
T.STATION(mask) = {'Apple'} ;
Accepted Answer
Categories
Find more on Logical 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!