Info

This question is closed. Reopen it to edit or answer.

Determine whether file value is within certain integer of an array value

1 view (last 30 days)
I have some arbitrary files in a directory "my_directory":
file_1002.txt
file_1012.txt
file_1023.txt
file_2045.txt
each with some value.
In Matlab, I have an array of values, call it "my_array"
1000
1010
2055
I want to check whether the file value is within 10 of a value in my array. Then, if so, I want to rename the file.
For example, file_1002.txt has a value of 1002, which is within 10 of values in my_array (namely 1000 and 1010). Therefore, file_1002.txt will be renamed file_1002_new.txt. Likewise, for 1012 and 2045. However, file_1023.txt has a value of 1023, which is not within 10 of any values in my_array, so file_1023.txt does not need to be renamed.
How could this scheme be accomplished? I am dealing with many files in my_directory and many values in my_array, so a quicker solution would be more optimal. Thanks!

Answers (1)

David Hill
David Hill on 22 Oct 2019
Load text into a variable say T and make sure it is a number.
if sum(my_array>(T-10)&my_array<(T+10))%repeat for as many files as you have
%save renamed file
end

Tags

Community Treasure Hunt

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

Start Hunting!