Unrecognized function or variable 'printstat'. Error in manet (line 162) if printstat == 1
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
Getting Unrecognized function or variable 'printstat'. error. Please can anyone help? Thanks.
%% print statistics
if printstat == 1
simstat(ini.globals.SIMTIME,Nodes,ini.globals.SENDERS,ini.globals.RECEIVERS,Protocols,Apps);
end
Accepted Answer
albara
on 29 Apr 2023
It looks like you're trying to use the variable printstat in your MATLAB script but it hasn't been defined yet. That's why you are getting the error "Unrecognized function or variable 'printstat'." To fix this error, you need to define the variable printstat before using it in the conditional statement.
For example, if you want to use the printstat variable as a flag to decide whether to print statistics or not, you can set it to 1 (true) or 0 (false). Add the following line before the conditional statement:
printstat = 1; % Set printstat to 1 (true) to print statistics, 0 (false) otherwise
Your updated code should look like this:
printstat = 1; % Set printstat to 1 (true) to print statistics, 0 (false) otherwise
%% print statistics
if printstat == 1
simstat(ini.globals.SIMTIME,Nodes,ini.globals.SENDERS,ini.globals.RECEIVERS,Protocols,Apps);
end
Now your script should run without encountering the "Unrecognized function or variable 'printstat'" error.
Waiting for your feedback
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes
8 Comments
onye erim
on 29 Apr 2023
Thanks, I no longer get the printstat error by using your updated code. But I now have an Unrecognized field name "SENDERS". error. Can you please look at this? Thanks.
albara
on 29 Apr 2023
The "Unrecognized field name 'SENDERS'" error suggests that the field SENDERS is not defined in the structure ini.globals. You should double-check the ini.globals structure to ensure that the field name is correct and exists.
First, you can display the contents of the ini.globals structure by adding the following line before the %% print statistics section:
disp(ini.globals);
This will show you the structure fields and their corresponding values. Verify if there is a field named SENDERS or if it has a different name. If the field name is different, update your code with the correct field name.
For example, let's say the correct field name is NUM_SENDERS. Your updated code should look like this:
printstat = 1; % Set printstat to 1 (true) to print statistics, 0 (false) otherwise
%% print statistics
if printstat == 1
simstat(ini.globals.SIMTIME,Nodes,ini.globals.NUM_SENDERS,ini.globals.RECEIVERS,Protocols,Apps);
end
If the field SENDERS doesn't exist in the ini.globals structure, you'll need to either define it or find out where it should be coming from. Make sure to carefully review your code or any related documentation to ensure that the correct variable or field name is being used.
Walter Roberson
on 29 Apr 2023
Walter Roberson yes I am. Any help please? The simulation also takes hours to run. It has a notice that says ''variable appears to change size on every loop iteration ( witin a script). Consider preallocating for speed ''. I dont know how to do that. Please any pointers? Thanks
onye erim
on 30 Apr 2023
albara I have diplayed it and it is not included in iniglobal. The simulation also takes hours to run. It has a notice that says ''variable appears to change size on every loop iteration( witin a script). Consider preallocating for speed ''. I dont know how to do that. Please any pointers? Thanks
albara
on 30 Apr 2023
It seems that you are facing two separate issues here:
- The missing SENDERS field in ini.globals.
- A variable that changes size on every loop iteration, causing the simulation to run slowly.
For the first issue, since SENDERS is not in ini.globals, you should either define it or figure out where it should come from. Please review your code and related documentation to ensure that the correct variable or field name is being used.
For the second issue, the message suggests that a variable's size changes during every loop iteration, which can cause the simulation to run slowly. Preallocating memory for a variable before entering the loop can significantly improve the performance. Here's a general approach to preallocate memory for an array in MATLAB:
- Identify the variable that changes size during each loop iteration. Typically, this is an array that grows with each iteration (e.g., result = [result; newValue];).
- Estimate the final size of the array (i.e., the size after all iterations).
- Preallocate memory for the array before the loop using the zeros or ones function based on the estimated size.
- During the loop, update the preallocated array instead of changing its size.
Here's an example of how to preallocate memory for an array:
% Example: Preallocate memory for an array
num_iterations = 1000;
result = zeros(num_iterations, 1); % Preallocate memory for a 1000x1 array
for i = 1:num_iterations
newValue = i^2; % Just an example, replace this with your actual computation
result(i) = newValue; % Update the preallocated array
end
To apply this in your specific case, you'll need to identify the variable that changes size during each iteration and apply the preallocation steps accordingly.
If you're unsure which variable is causing the performance issue, you can use MATLAB's Profiler to identify the slowest parts of your code. To start the Profiler, type profile on before running your script, and profile off after the script finishes. Then, type profile viewer to see the performance report.
Please let me know if you need further assistance with these issues.
ini is created by
ini = ini2struct('config.ini')
so the implication is that SENDERS should be part of config.ini
Walter Roberson
on 30 Apr 2023
The code expects to use ini.globals.SENDERS but the config.ini file defines SENDERS in the constants section, ini.constants.SENDERS
The config.ini file defines printstats in the visuals section, so the reference should be to ini.visuals.printstats
More Answers (0)
Categories
Find more on Performance and Memory in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)