Clear Filters
Clear Filters

How to delete all strings with NaN in Struct

9 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 1 Aug 2023
Edited: Stephen23 on 1 Aug 2023
"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 on 3 Aug 2023
Hi tanks for the support. As I answered in a comment below I was able to solve the problem. Thank you for your time! :)

Sign in to comment.

Accepted Answer

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 1 Aug 2023
  • 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 Comment
Volker
Volker on 3 Aug 2023
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 :)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!