필터 지우기
필터 지우기

How to delete all strings with NaN in Struct

조회 수: 9 (최근 30일)
Volker
Volker 2023년 8월 1일
댓글: Volker 2023년 8월 3일
Hello,
I've been wasting hours trying to remove the rows in the struct variable ID with value = 'NaN' in osmScenarios.trafficLight.ID.
ID is a string in this case.
For example:
arep = strrep(osmScenarios.trafficLight.ID,'NaN','')
error: Too many input arguments.
I know that probably something is wrong with me using that struct but what the change to happen in that struct. No, I need it to happen in that struct.
Please help. i don´t know what to do anymore.
Best regards
  댓글 수: 3
Stephen23
Stephen23 2023년 8월 1일
편집: Stephen23 2023년 8월 1일
"strrep(osmScenarios.trafficLight.ID,'NaN','') error: Too many input arguments."
Most likely the field TRAFFICLIGHT is non-scalar, thus the dot-indexing TRAFFICLIGHT.ID produces a comma-separated list with multiple arrays. If that is the case, you might need to use a loop+indexing, or perhaps ARRAYFUN... what exactly you need depends on the sizes and arrangement of the arrays stored in that structure, which you have not explained nor uploaded. Not uploading your actual data will significantly slow down your getting a useful solution.
"I've been wasting hours trying to remove the rows in the struct variable ID with value = 'NaN' ..."
It is unclear how STRREP would help you to "remove the rows in the struct variable" because it does not do anything like "remove the rows" of a structure, it simply replaces some characters with some other characters (just as its documentation states). So as well as providing your actual data, you need to explain more clearly what you want to achieve, preferably with both the input data and expected output data.
Volker
Volker 2023년 8월 3일
Hi tanks for the support. As I answered in a comment below I was able to solve the problem. Thank you for your time! :)

댓글을 달려면 로그인하십시오.

채택된 답변

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2023년 8월 1일
  • Based on your description, it seems like you have a struct variable osmScenarios, and within it, there is a field trafficLight that contains another field ID. The values in the ID field are strings, and you want to remove the rows where the value is 'NaN'. However, the 'NaN' values in strings cannot be directly removed using strrep since it's designed for replacing substrings in strings.
  • To remove the rows with 'NaN' values in the ID field of the osmScenarios.trafficLight struct, you can do the following:
% Assuming osmScenarios is the struct variable
for i = numel(osmScenarios.trafficLight):-1:1
if strcmp(osmScenarios.trafficLight(i).ID, 'NaN')
osmScenarios.trafficLight(i) = [];
end
end
Here's what the code does:
  1. We loop through the elements of osmScenarios.trafficLight in reverse order (from end to start) using numel to get the number of elements. This is done to ensure that the removal of elements does not affect the index of subsequent elements.
  2. We check if the ID field of the current element is equal to 'NaN' using strcmp.
  3. If the condition is true, we remove the element from the osmScenarios.trafficLight struct using indexing (osmScenarios.trafficLight(i) = []).
  • After running this code, the rows with 'NaN' in the ID field will be removed from the osmScenarios.trafficLight struct.
  • Note: If the 'NaN' values are actual NaN numeric values instead of strings, you can use the isnan function to identify and remove those rows. But based on your description, it seems like you are dealing with string values of 'NaN'.
  댓글 수: 1
Volker
Volker 2023년 8월 3일
Thank you for your support! I actually had this soulution but as a newby in programming I didn´t think about using a reversed loop. I my case a loop from from 1:... doesn´t work because after deleting an element the next element would change index. With reversing the loop I solved this problem. Your answer is correct though since I didn´t mention this in my question :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by