Unrecognized function or variable 'printstat'. Error in manet (line 162) if printstat == 1
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
채택된 답변
albara
2023년 4월 29일
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
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.
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 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
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
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
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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
