Problem with Code for my coursera course

4 vues (au cours des 30 derniers jours)
Hasnat Mosharraf
Hasnat Mosharraf le 26 Jan 2025
This is the question I am trying to solve and I also uploading the code i have written and error I am getting, Please help me the error please.

Réponse acceptée

Hasnat Mosharraf
Hasnat Mosharraf le 28 Jan 2025
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.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 26 Jan 2025
The printout shows that the table has a variable name Region but no variable named Regions
  5 commentaires
Hasnat Mosharraf
Hasnat Mosharraf le 28 Jan 2025
I have tried using stormdata.stormdata.Region. It says unrecognized field 'stormData'
Walter Roberson
Walter Roberson le 28 Jan 2025
You do not need both
load("stormData");
and
stormData = load("stormData");
You can probably get rid of the second load()

Connectez-vous pour commenter.


Umar
Umar le 27 Jan 2025

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:

  • Line 3: The syntax numStorms height (stormData); is incorrect. It seems like you intended to assign the number of storms to numStorms.
  • Line 4: The syntax stormyRegion mode (stormData.Regions); is incorrect. The correct function call should use an assignment operator and proper function syntax.
  • Line 6: The function mean is called with an incorrect parameter 'omitan'. The correct parameter should be 'omitnan' to ignore NaN values.
  • Line 7: The function med is not a valid MATLAB function. The correct function is median.

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.

  3 commentaires
Umar
Umar le 27 Jan 2025

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

https://www.mathworks.com/help/matlab/ref/double.median.html?searchHighlight=Median&s_tid=srchtitle_support_results_1_Median

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.

Image Analyst
Image Analyst le 27 Jan 2025
load returns a structure or structure array, unless the filename is a text file of only numbers (no header) in which case it returns a double array. It does not return a table. Each variable is a member (field) of the structure. Since it's not a table, you wouldn't use height.
I see equals signs in the screenshot of his code so I'm not sure how you didn't have them in your version of his code, unless he edited it and replaced the screenshot.
Not sure what the last image in your comment was supposed to be but it shows up as a missing image symbol.
Otherwise it's a pretty comprehensive explanation. 🙂
Rather than use ChatGPT, you can use the Mathworks one https://www.mathworks.com/matlabcentral/playground?s_tid=gn_mlc_playground (also on blue menu banner above on this page).
which I believe uses the Microsoft LLM with some tweaks to make it more MATLAB-Code-aware.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Timing and presenting 2D and 3D stimuli dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by