I am working on code and it works when I load up MATLAB, but when I add code the previous code that works no longer does.

1 view (last 30 days)
I have this code:
clear;clc
data = import('StormEvents2013.csv')
Error using import
Import argument 'StormEvents2013.csv' cannot be found or cannot be imported.
data = data(~ismissing(data.Month),:)
monthorder = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'}
data.Month = reordercats(data.Month, monthorder)
histogram(data.Month)
topEvents = data(data.Month == 'June',:)
histogram(topEvents.Event_Type)
topEvents.Event_Type = renamecats(topEvents.Event_Type,'Astronomical Low Tide','Astronomical Low Tide');
topEvents.Event_Type = removecats(topEvents.Event_Type,{'Astronomical Low Tide','Winter Weather','Winter Storm','Sleet','Marine Strong Wind','Marine High Wind','Lake-Effect Snow','Ice Storm','Hurricane','Heavy Snow','Freezing Fog','Extreme Cold/Wind Chill','Dense Smoke','Cold/Wind Chill','Coastal Flood','Blizzard','Avalanche'})
histogram(topEvents.Event_Type)
topEvents.Event_Type = addcats(topEvents.Event_Type,{'<New Category>'});
topEvents.Event_Type = renamecats(topEvents.Event_Type,'<New Category>','Marine Events');
topEvents.Event_Type = mergecats(topEvents.Event_Type,{'Marine Events','Waterspout','Marine Thunderstorm Wind','Marine Hail','Marine Dense Fog','High Surf'});
topEvents.Event_Type = mergecats(topEvents.Event_Type,{'Lightning'})
histogram(topEvents.Event_Type)
groupcount = groupsummary(topEvents, "Event_Type")
groupcount = sortrows(groupcount,'GroupCount','descend')
%Thunderstorm wind happened the most with 3838 occurences
topEvent = topEvents(topEvents.Event_Type == 'Thunderstorm Wind',:)
geodensityplot(topEvent.Begin_Lat, topEvent.Begin_Lon)
title("Distribution of Thunderstowm Winds in 2013")
hold on
secondevent = topEvents(topEvents.Event_Type == 'Hail',:)
geodensityplot(secondevent.Begin_Lat, secondevent.Begin_Lon, "FaceColor",'red')
hold off
and it works until i add the last part with the geodensity plots. Once I add them, the error message says that there was an error running histogram because adding cartesian plots and geoaxes is not supported. I cannot add the file to upload because it is too big. Sorry, but if you happen to know the issue then please tell me.

Answers (2)

Walter Roberson
Walter Roberson on 5 Nov 2022
it works until i add the last part with the geodensity plots. Once I add them, the error message says that there was an error running histogram because adding cartesian plots and geoaxes is not supported.
That is correct. geodensity plots use latitude and longitude, and are implemented using carteasian axes, which are not compatible with ordinary numeric axes.
In the code before that point you have several histogram() calls, but you do not have any hold statement. If you are using original MATLAB scripts, then each of those histograms() without "hold on" would erase the contents of the current axes, and then would draw the new histogram. You then have a geodensityplot() plot call, and if "hold on" is not in effect (as implied by your code) then the geodensityplot() call would also erase the current axes content and create the new axes. You then turn hold on, and draw another geodensityplot() and that should be fine because the active plot is a geodensityplot() already.
So, the code as posted should work -- but if you are using regular scripts you would not see any of the histogram() outputs because later calls overwrite them before they are displayed. If you were using LiveScript then you should see those plots.
So what is going wrong?
Answer: Somehow, you ended up with "hold on" in effect, during your development and debugging. And that caused MATLAB to try to combine a geodensity plot into the same axes as a numeric plot, and that is a problem.
The code as posted is not (should not be) the problem, but a left-over "hold on" from other work is giving you problems.

Suvansh Arora
Suvansh Arora on 4 Nov 2022
Here are two workarounds you can try:
  1. Use "readtable" function to import csv file rather than use import data button
  2. Save your csv file as xls file and import the xls file with import data button
In order to know how to use “readtable” with “csv”, please follow this documentation:

Categories

Find more on Discrete Data Plots 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!