MATLAB Answers

grant
0

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

Asked by grant
on 22 Oct 2019 at 0:33
Latest activity Answered by David Hill on 22 Oct 2019 at 0:48
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!

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by David Hill on 22 Oct 2019 at 0:48

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

  0 Comments

Sign in to comment.