For this specific problem, Loading the stormData table twice created the problem.
It was not a problem regarding Matlab syntax.
Thank you all for the reply.
Hi @Hasnat Mosharraf,
The script you provided is intended to load storm data and perform some statistical analysis. However, it contains several errors that need to be addressed for it to function correctly. Let's break down the script line by line, identify the issues, and provide a corrected version.
load("stormData"); stormData = load("stormData"); numStorms height (stormData); stormyRegion mode (stormData.Regions); stormyState= mode (stormData.State); aveCost = mean (stormData. Total_Cost, 'omitan'); medCost = med (stormData. Total_Cost, 'omitan');
Issues Identified
Redundant Loading: Line 1 and Line 2 both attempt to load the same data. You only need one of these lines.
Syntax Errors:
Field Name Error: The error message indicates that stormData does not contain a field named Regions. This could be due to a typo or because the data structure does not have this field. Here is a revised version of the script with corrections applied:
% Load the storm data stormData = load("stormData");
% Count the number of storms numStorms = height(stormData); % Assuming stormData is a table
% Determine the most common region and state stormyRegion = mode(stormData.Regions); % Ensure 'Regions' is a valid field stormyState = mode(stormData.State); % Ensure 'State' is a valid field
% Calculate average and median costs, ignoring NaN values aveCost = mean(stormData.Total_Cost, 'omitnan'); % Corrected parameter medCost = median(stormData.Total_Cost, 'omitnan'); % Corrected function
Explanation of Corrections
Loading Data: The script now only loads the data once, which is efficient and avoids redundancy.
Correct Syntax: * The assignment of numStorms now correctly uses the height function to get the number of rows in the stormData table. * The mode function is now correctly assigned to stormyRegion and stormyState.
Handling NaN Values: The use of 'omitnan' in the mean and median functions ensures that any missing values in the Total_Cost field do not affect the calculations.
Field Validation: Before running the script, ensure that the fields Regions and State exist in the stormData structure. You can check the structure of stormData using the command fieldnames(stormData) to confirm the available fields.
By addressing the syntax errors and ensuring that the field names are correct, the revised script should now run without errors. Always validate the structure of your data before performing operations to avoid runtime errors. If you encounter further issues, consider checking the contents of stormData to ensure it contains the expected fields.
Hi @Hasnat Mosharraf ,
It is waste of time relying on such resources. You will not learn if you don’t learn from the bug in code. I went through screenshots provided by you and implemented the code. Here is a breakdown of the code provided along with step by step explanation.
% Create a table for storm data stormData = table(... {'KANSAS'; 'KANSAS'; 'KANSAS'; 'KANSAS'; 'TENNESSEE'; 'TENNESSEE'; 'MICHIGAN'; 'MICHIGAN'; 'MICHIGAN'; 'MICHIGAN'}, ... % State {'June'; 'June'; 'June'; 'June'; 'May'; 'May'; 'May'; 'May'; 'May'; 'May'}, ... % Month {'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'; 'Thunderstorm Wind'}, ... % Event Type {'06/15/2013 14:50'; '06/15/2013 13:58'; '06/15/2013 14:29'; '06/15/2013 14:40'; '05/21/2013 18:15'; '05/21/2013 18:30'; '05/21/2013 19:20'; '05/21/2013 19:24'; '05/30/2013 14:40'; '05/30/2013 15:55'}, ... % Date {'CST-6'; 'CST-6'; 'CST-6'; 'CST-6'; 'EST-5'; 'EST-5'; 'EST-5'; 'EST-5'; 'EST-5'; 'EST-5'}, ... % Time Zone {'06/15/2013 14:50'; '06/15/2013 13:58'; '06/15/2013 14:29'; '06/15/2013 14:40'; '05/21/2013 18:15'; '05/21/2013 18:30'; '05/21/2013 19:20'; '05/21/2013 19:24'; '05/30/2013 14:40'; '05/30/2013 15:55'}, ... % Event Time [500; 100; 0; 0; 0; 0; 10000; 0; 0; 0], ... % Total Cost [0; 0; 0; 0; 0; 0; 0; 0; 0; 0], ... % Injuries [38.91; 38.53; 38.98; 38.89; 36.21; 36.12; 42.35; 42.35; 42.35; 42.35], ... % Latitude [-94.63; -94.87; -94.67; -94.81; -83.3; -83.49; -83.42; -83.42; -83.42; -83.42], ... % Longitude {'South'; 'South'; 'South'; 'South'; 'South'; 'South'; 'UpperMidwest'; 'UpperMidwest'; 'UpperMidwest'; 'UpperMidwest'}, ... % Region {'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'; 'Continental'}, ... % Climate [500; 100; 0; 0; 0; 0; 10000; 0; 0; 0] ... % Total Cost (again for clarity) );
This section initializes the stormData table with various attributes related to thunderstorm events. Each column represents a different aspect of the storm data.
% Assign variable names to the table for clarity stormData.Properties.VariableNames = {'State', 'Month', 'EventType', 'Date', 'TimeZone', 'EventTime', 'TotalCost', 'Injuries', 'Latitude', 'Longitude', 'Region', 'Climate', 'TotalCostAgain'};
Here, assigning meaningful names to the columns of the table, enhancing readability and usability.
% Convert State and Region to categorical for mode calculation stormData.State = categorical(stormData.State); stormData.Region = categorical(stormData.Region);
This code converts the State and Region columns to categorical data types, which is beneficial for statistical analysis, particularly when calculating modes.
% 1. Find the number of recorded thunderstorms numStorms = height(stormData);
This line calculates the total number of recorded thunderstorms by determining the height (number of rows) of the stormData table.
% 2. Find the region with the most storms stormyRegion = mode(stormData.Region);
The mode function identifies the region with the highest frequency of storms.
% 3. Find the state with the most storms stormyState = mode(stormData.State);
Mode function
https://www.mathworks.com/help/matlab/ref/double.mode.html?overload=mode+false
Similarly, this line finds the state with the most recorded thunderstorms.
% 4. Calculate mean total cost of all thunderstorms omitting NaN entries aveCost = mean(stormData.TotalCost, 'omitnan');
Mean Function
https://www.mathworks.com/help/matlab/ref/double.mean.html?s_tid=doc_ta
This line computes the average total cost of thunderstorms, excluding any NaN values.
% 5. Calculate median total cost of all thunderstorms omitting NaN entries medCost = median(stormData.TotalCost, 'omitnan');
Median function
Here, the median total cost is calculated, also omitting NaN values.
% Display results fprintf('Number of recorded thunderstorms: %d\n', numStorms); fprintf('Region with the most storms: %s\n', char(stormyRegion)); fprintf('State with the most storms: %s\n', char(stormyState)); fprintf('Average total cost of thunderstorms: %.2f\n', aveCost); fprintf('Median total cost of thunderstorms: %.2f\n', medCost);
This section outputs the results of the analysis to the console, providing a clear summary of the findings.
% Comparison of mean and median if aveCost > medCost fprintf('The mean cost is greater than the median cost, indicating a right- skewed distribution.\n'); elseif aveCost < medCost fprintf('The median cost is greater than the mean cost, indicating a left- skewed distribution.\n'); else fprintf('The mean and median costs are equal, indicating a symmetric distribution.\n'); end
Finally, this conditional block compares the mean and median costs, providing insights into the distribution of the total costs.
Please see attached.
Hope this helps.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!