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

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

回答 (2 件)

VBBV
VBBV 2022 年 11 月 19 日
編集済み: VBBV 2022 年 11 月 19 日
generateReport(allHurricaneData) % are you calling structure in this way in main script
or
generateReport(HurricaneData) % this does not exist

7 件のコメント

Me
Me 2022 年 11 月 19 日
I am calling the function generateReport(allHurricaneData) in the main script. THe structure is in the function getAllHurricaneData. The generateReport function is using the output allHurricaneData from the getAllHurricaneData function to get specific values to print to a new CSV file.
VBBV
VBBV 2022 年 11 月 19 日
What are input arguments to function getAllHurricaneData ? can you attach fhe relevant function files ?
Me
Me 2022 年 11 月 19 日
getAllHurricaneData :
function allHurricaneData = 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
allHurricaneData = HurricaneData;
end
calcCategory:
function [category, color] = calcCategory(wind)
% wind strength determines hurricane category and color
% INPUT: wind - a number for the wind speed (mph) of particular hurricane
% data point
% OUTPUTs: category - an integer between 0-5 for hurricane category based
% on wind speed
% color - triple value RGB color
%
% category 0: <=38, color [0 1 1], turquoise
% category 0: 39-73, color [0.25 1 0], green
% category 1: 74-95, color [1 1 0], yellow
% category 2: 96-110, color [0 0.75 0], orange
% category 3: 111-129, color [1 0 0], red
% category 4: 120-156, color [0.75 0 1], violet
% category 5: >=157, color [1 0 1], pink
if wind <= 38
category = 0;
color = [0 1 1];
end
if wind >= 39 && wind <= 73
category = 0;
color = [0.25 1 0];
end
if wind >= 74 && wind <= 95
category = 1;
color = [1 1 0];
end
if wind >= 96 && wind <= 110
category = 2;
color = [0 0.75 0];
end
if wind >= 111 && wind <= 129
category = 3;
color = [1 0 0];
end
if wind >= 130 && wind <= 156
category = 4;
color = [0.75 0 1];
end
if wind >=157
category = 5;
color = [1 0 1];
end
end
VBBV
VBBV 2022 年 11 月 19 日
編集済み: VBBV 2022 年 11 月 19 日
Z(Y) % e.g. Z = generateReport , Y = HurricaneData
Unrecognized function or variable 'Y'.
Y = X % getAllHurricaneData
function Z(Y)
Y.name
end
function Y = X
B.name = 'H';
B.value = 120;
Y = B
end
can you check which order you are calling functions generateReport and getAllHurricaneData in the main script ? May be you are not calling in correct sequence
Me
Me 2022 年 11 月 19 日
This is the order in which I am calling them:
allHurricaneData = getAllHurricaneData('allHurricanes.csv');
generateReport(allHurricaneData)
Walter Roberson
Walter Roberson 2022 年 11 月 19 日
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 2022 年 11 月 29 日
編集済み: VBBV 2022 年 11 月 29 日
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

サインインしてコメントする。

Kalil
Kalil 2022 年 11 月 28 日
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 件のコメント

Walter Roberson
Walter Roberson 2022 年 11 月 28 日
Is there a particular reason why you ask for two outputs from max() but ignore the second output ?
Kalil
Kalil 2022 年 11 月 28 日
編集済み: Kalil 2022 年 11 月 28 日
Just remove the outside max(). And I don't want generateReport calling "color" for efficiency.
Walter Roberson
Walter Roberson 2022 年 11 月 28 日
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 2022 年 11 月 28 日
Doesn't matter much. Either work fine. It's just so that I remember that there's another output not being used.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

製品

質問済み:

Me
2022 年 11 月 19 日

編集済み:

2022 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by