How do I fix the particular error code that I was given for my code?

3 vues (au cours des 30 derniers jours)
Me
Me le 19 Nov 2022
Modifié(e) : VBBV le 29 Nov 2022
I am working on an assignment where I have a main script and several functions. In one particular function I am working on, I need to call from a structure array I made in a previous function that is also located in the main script. I also need to call a single output from a two output function I made. I am receiving an error and I do not know what it means or how to fix it. Please let me know if there is additional information needed (like the mainscript or other functions). Even without the error, I feel my code may be incorrect. I can't even check, however. Help would be greatly appreciated, as I have been struggling with this for a while. Thank you in advance!
Here is the error:
Unrecognized function or variable 'HurricaneData'.
Error in generateReport (line 14)
name = HurricaneData(range).name;
Here is the function Code:
function generateReport(allHurricaneData)
% uses the hurricane data to create a report
% INPUT: allHurricaneData - structure array that has the data for all the
% hurricanes with six fields: name, data, Xs, Ys, wind, pressure
% OUTPUT: a report stored in a CSV file called hurricaneReport.csv where
% each row has the following information: name of hurricane,
% first date, last date, max wind, max pressure, max category
% e.g. for Cindy: Cindy, Jun 19, Jun 24, 60, 1004, 0
hurricaneReportfid = fopen('hurricaneReport.csv', 'w');
% loop through 13 names
for range = 1:13
name = allHurricaneData(range).name;
firstDate = string(allHurricaneData(range).date{1,1});
lastDate = string(allHurricaneData(range).date{30,1});
maxWind = max(allHurricaneData(range).wind);
maxPressure = max(allHurricaneData(range).pressure);
category = (category == calcCategory(maxWind));
fprintf(hurricaneReportfid,'%s, %s, %s, %d, %d, %d,\n', name, firstDate, lastDate,maxWind, maxPressure, category);
end
fclose('all');
end

Réponses (2)

VBBV
VBBV le 19 Nov 2022
Modifié(e) : VBBV le 19 Nov 2022
generateReport(allHurricaneData) % are you calling structure in this way in main script
or
generateReport(HurricaneData) % this does not exist
  7 commentaires
Walter Roberson
Walter Roberson le 19 Nov 2022
Error in generateReport (line 14)
name = HurricaneData(range).name;
The line in the error message does not exist in the code you posted. Line 14 of generateReport is
lastDate = string(allHurricaneData(range).date{30,1});
and the closest similar line is line 12 of the file which contains
name = allHurricaneData(range).name;
You need to figure out how it managed to run a line of code that does not exist in the file you think you are executing. You should use
which -all generateReport
to see if you are accidentally invoking a different generateReport.m than you think you are. And you should probably
clear generateReport
just in case there was an old version of it stored in memory.
VBBV
VBBV le 29 Nov 2022
Modifié(e) : VBBV le 29 Nov 2022
Since you are assigning HurricaneData structure to allHurricaneData variable as the output in the below function, whenver this function is called or run
getAllHurricaneData(allHurricanesFileName)
it creates the structure HurricaneData but does not save or store the data into workspace each time. So, each time you call generateReport function to access allHurricaneData,
function generateReport(allHurricaneData)
it refers or points to the data present in variable HurricaneData which does not exist in workspace anymore since variables defined inside function calls are not stored in workspace. So you need to use the same name as shown below as the ouput for the function getAllHurricaneData
function generateReport(HurricaneData) % use same name
in order to access whole data and avoid that error.
function generateReport(HurricaneData) % use same name
% uses the hurricane data to create a report
% INPUT: allHurricaneData - structure array that has the data for all the
% hurricanes with six fields: name, data, Xs, Ys, wind, pressure
% OUTPUT: a report stored in a CSV file called hurricaneReport.csv where
% each row has the following information: name of hurricane,
% first date, last date, max wind, max pressure, max category
% e.g. for Cindy: Cindy, Jun 19, Jun 24, 60, 1004, 0
hurricaneReportfid = fopen('hurricaneReport.csv', 'w');
% loop through 13 names
for range = 1:13
name = HurricaneData(range).name; % use same name
firstDate = string(HurricaneData(range).date{1,1}); % use same name
lastDate = string(HurricaneData(range).date{30,1});
maxWind = max(HurricaneData(range).wind);
maxPressure = max(HurricaneData(range).pressure);
category = (category == calcCategory(maxWind));
fprintf(hurricaneReportfid,'%s, %s, %s, %d, %d, %d,\n', name, firstDate, lastDate,maxWind, maxPressure, category);
end
fclose('all');
end
function HurricaneData = getAllHurricaneData(allHurricanesFileName)
% takes the NAME of an input CSV file that has a single column with the names
% of the individual hurricanes CSV files and extracts hurricane data from
% the files into a structure Aarray
% INPUT: allHurricanesFileName - char array, which is the name of input
% CSV file
% RETURNS: allHurricaneData - structure array that has the data for all
% the hurricanes with six fields: name, date, Xs, Ys, wind, pressure
a = readcell(allHurricanesFileName);
for x = 1:size(a)
c = a{x,1};
d = readmatrix(c);
e = readtable(c);
HurricaneData(x).name = c(1:end-4);
HurricaneData(x).date = e(1:end,:);
HurricaneData(x).Xs = d(:,5);
HurricaneData(x).Ys = d(1:end,6);
HurricaneData(x).wind = d(1:end,7);
HurricaneData(x).pressure = d(1:end,8);
end
% HurricaneData = HurricaneData;
end

Connectez-vous pour commenter.


Kalil
Kalil le 28 Nov 2022
Use
[category, ~] = max(calcCategory(max(allHurricaneData(range).wind)))
This will grab the max category for each hurricane.
Also use
firstDate = string(allHurricaneData(range).date{2,1});
lastDate = string(allHurricaneData(range).date{end,1});
To get the first and last dates.
  4 commentaires
Walter Roberson
Walter Roberson le 28 Nov 2022
I am referring to the fact you used
[category, ~] = max(STUFF)
instead of
category = max(STUFF)
I am trying to understand why you have the ignored second output there?
Kalil
Kalil le 28 Nov 2022
Doesn't matter much. Either work fine. It's just so that I remember that there's another output not being used.

Connectez-vous pour commenter.

Catégories

En savoir plus sur App Building dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by