geoshow of struct array

6 次查看(过去 30 天)
Poulomi Ganguli
Poulomi Ganguli 2024-8-27
评论: Umar 2024-8-28
Hello
I wish to plot the attached struct array, which is clipped from a shapefile using mapping tools available in MATLAB. Also, I wish to shade each code available in the field USA_SSHS with particular shade for each of these lines, for example, -1: for blue, 0: for green, 1: yeallow, 2: red, & so on. Can anyone suggest how to do so?
  3 个评论
Umar
Umar 2024-8-28
Hi Poulomi Ganguli,
Please let me know if the code below has resolved your problem.

请先登录,再进行评论。

回答(1 个)

Umar
Umar 2024-8-28

Hi @Poulomi Ganguli ,

Let me address your query regarding, “I wish to plot the attached struct array, which is clipped from a shapefile using mapping tools available in MATLAB. Also, I wish to shade each code available in the field USA_SSHS with particular shade for each of these lines, for example, -1: for blue, 0: for green, 1: yeallow, 2: red, & so on. Can anyone suggest how to do so? “

Please see my response to your comments below.

To plot the struct array `NI_BB_2020.mat` in MATLAB and shade each code available in the field `USA_SSHS` with specific colors, you can follow these steps. The process involves loading the data, extracting the relevant fields, and then plotting with appropriate color coding based on the values in `USA_SSHS`. Here is the example code snippet,

    data = load('NI_BB_2020.mat');
    % Define color mapping
    colorMap = containers.Map([-1, 0, 1, 2], {'b', 'g', 'y', 'r'}); % blue, green, yellow,   
    red
    % Extract unique codes from USA_SSHS
    uniqueCodes = unique([data.NI_BB_2020.USA_SSHS]);
    figure; % Create a new figure
    hold on; % Hold on to plot multiple lines
    for i = 1:length(data.NI_BB_2020)
      lat = data.NI_BB_2020(i).Lat; % Extract latitude
      lon = data.NI_BB_2020(i).Lon; % Extract longitude
      % Determine color based on USA_SSHS value
      sshsValue = data.NI_BB_2020(i).USA_SSHS;
      if isKey(colorMap, sshsValue)
        color = colorMap(sshsValue);
      else
        color = 'k'; % Default to black if not found in map
      end
      % Plot with specified color
      plot(lon, lat, 'Color', color, 'LineWidth', 2); 
    end
    hold off; % Release hold on plotting
    xlabel('Longitude');
    ylabel('Latitude');
    title('Plot of NI BB 2020 with Shaded USA_SSHS Codes');
    % Create a legend using the color values instead of keys
    legend(colorMap.values(), 'Location', 'Best'); % Add legend for clarity
    grid on; % Optional: add grid for better visualization

Please see attached.

For more information on “unique” function, please refer to,

https://www.mathworks.com/help/matlab/ref/double.unique.html

So, if you look at the code, I use load function which imports the data from the specified .mat file. A containers.Map is created to associate specific USA_SSHS values with colors. This allows for easy visualization based on the data's categorical values. Afterwards, used unique code function which retrieves distinct USA_SSHS values from the dataset, which can be useful for understanding the range of data. The for loop iterates through each entry in the dataset, extracting latitude and longitude. It checks the USA_SSHS value against the colorMap to determine the appropriate color for plotting. Then, used plot function is used to create a scatter plot of the latitude and longitude, with the specified color and line width. Finally, after plotting all points, the script adds labels, a title, and a legend to enhance the plot's clarity and usability.

Hope this answers your question, please let me know if you have any further questions.

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by