read newest file in loop

7 views (last 30 days)
t33dot
t33dot on 10 Nov 2013
Commented: t33dot on 10 Nov 2013
Hi, I've been working on matlab trying to make a while loop that would read the newest file from a live feed directory, and would stop when there are no new files available.
The whole code is quite big, so I won't post it, but I believe the main problem is this piece here:
dir1=fileList;
[~,dx] = sort([dir1.datenum],'descend');
newest = dir1(dx(end)).name; % returns newest file
which works perfect by itself, but when used more than once it returns the error:
"Dot name reference on non-scalar structure." on "[~,dx] = sort([dir1.datenum],'descend');"
Also tried using
d1 = fileList;
dates = [d1.datenum];
[~, newestIndex] = max (dates);
d1(newestIndex);
d1.date % will display long list of files and stuff, consider deleting...
newest = d1(newestIndex(end)).name;
returns same error.
I've also tried using just the last line i.e.
newest = dir1(dx(end)).name; % returns newest file
But does not seem to refresh the dir in question.
To clarify: - code will check for the newest file available (image) - after process, refresh and check for newest file again, use strcmp to compare filenames and see if new file exists.
- if it does --> loop: run as long as new files arrive.
Hope its more clear now...
Currently, does not appear to discover any newer files than the first instance.. so it doesn't even reach the loop.
Any help is appreciated!

Answers (1)

Walter Roberson
Walter Roberson on 10 Nov 2013
You are overwriting FileList with something that is not the result of dir() . For example you might be assigning to it a list of file names at some point.
  4 Comments
t33dot
t33dot on 10 Nov 2013
im sorry, getallfiles () is wrongly named its actually used to remove the hidden or bad files, the . and ..
when I don't use it, 'newest' always returns .. instead of an actual file
'feed' is the absolute path, i.e. feed = 'D:\Pictures\...', and it remains the same throughout.
Tried clear variables:
clear('fileList','dates','d1','d2','datenum','newestIndex','fileList2');
fileList = getAllFiles(feed);
d2 = fileList;
dates = [d2.datenum];
[~, newestIndex2] = max (dates);
d2(newestIndex2);
newest2 = d2(newestIndex2(end)).name; %Returns name of newest file.
save()
And get the same thing:
Dot name reference on non-scalar structure.
Error in improcc (line 205)
dates = [d2.datenum];
t33dot
t33dot on 10 Nov 2013
Good news! so I embedded this into a function, and use that instead. No more error! and loop works now. A joyous occasion :D. Seemed like a matlab bug to me...
I appreciate your help on this! Thanks again.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!