MATLAB Answers


array values are overwritten

Asked by swati mane on 19 Jul 2019
Latest activity Commented on by swati mane on 24 Jul 2019
Accepted Answer by Jan
I am working with for loop in my code. I am getting the answer but after 8 steps i.e. from the step 9, the value of array is overwritten.
means The value I was getting at column 2 in the first 8 steps , I am getting that value in the column 5 when i start with step 9.
please help me for the same.


Joel Handy on 19 Jul 2019
One option is to rename your files. If you named them something like Img001, img002, .., Img498, Img499, Img500, dir would return your file names in the right order.
You might be able to sort your images by date. The file info that dir gives you has the files' datestamp. That can be unreliable though if people are copying images around or opening and closing them. Anything that would update the files metadata.
The most reliable but most complicated option would be to parse you file names to extract the file number and sort that way.
fileNames = {}'
fileNumStr = regexp(fileNames, '[0-9]*', 'match');
fileNum = str2double([fileNumStr{:}]);
[~,sortIdx] = sort(fileNum);
imgFiles = imgFiles(sortIdx);
@joel handy
Thanks a ton ...its working perfectly.
Hello sir..
Now i have frames named as 000 to 505 .
I want to take these frames as 000 to 025 then 026 to 050 then 051 to 075 and so on.
Per second i should have 25 frames. Total time is 20 seconds. Thanks in advance.

Sign in to comment.

1 Answer

Answer by Jan
on 19 Jul 2019
 Accepted Answer

Prefer file names including leading zeros: image0001.jpg, image 0002.jpg etc. Then the numeical order equals the alphabetical order.
If you have the less useful naming scheme already, use to get it in order.


Sign in to comment.