MATLAB Answers


Load multiple files into GUI, in specific order

Asked by Plastic Soul on 29 May 2014
Latest activity Commented on by Plastic Soul on 30 May 2014
I was wondering, is there a way to load multiple files into MatLab GUI, in the order of their selection? For example, lets say I have files: one.txt, two.txt, three.txt, four.txt. Now, if I say:
[FileNames PathNames]=uigetfile('*.txt', 'Chose files to load:','MultiSelect','on');
FileNames = cellstr(FileNames);
for i=1:length(FileNames)
Path{i} = fullfile(PathNames, FileNames{i});
fileID{i} = fopen(Path{i});
while ~feof(fileID{i})
C = textscan(fileID{i},'\t%f',1000000);
Then, it will load files by alphabet, so when I plot, I will have on Xaxis: four, one, three, two . Therefore, I always have to rename files as: aone, btwo, cthree, dfour . Is it possible to avoid this and import files in the order of selection (by holding CTRL key)?
P.S. Dont mind if you spot an error in the code, I just gave it for better understanding.


Show 1 older comment
Geoff says "it appears", the doc reports that "it is" -- under the multiselect section is included: File names in the cell array are sorted in the order your platform uses.
I looked at the Windows SDK documentation here and it doesn't appear that the information of "click order" is even available in the full-fledged uicontrol; what more the Matlab wrapper function around the system control.
Only way I see you've got would be to write a customized control that used the optional hook to an event handler inside the uicontrol and I didn't dig deep enough to tell just what events are seen by it on the presumption your interest didn't delve that deeply. :)
OTOMH, if you allow multi-select, I don't see any way to get what you're looking for. This might be a question for MSDN (presuming again you're on Windoes).
Of course, if you don't allow multi-select but did arrange to return to the dialog after a single, then you can save them in the order returned...probably not something a user would fall in love with, however...
If there were always going to be a relatively small number of files, you could perhaps populate a listbox and allow the selection from it such that you could ascertain the order selected...again, seems kinda' clunky.
Not sure I see away other than perhaps to allow the multiselect, then present those selected and ask for them to be ordered by user. Other than that would seem would have to have some internal logic to be able to make an order that meets some criterion if there's some naming scheme or whatever.
I was able to select the same set of files and have them appear in a different order in the FileNames variable depending upon how the files were sorted in the uicontrol - alphabetically, by modified date, by size, etc. but not by the order in which the files were selected (by me). So it doesn't matter which order I select them in, it just depends upon the order in which they are listed in the uicontrol.
Yes, that's what the doc says...they're returned "sorted in the order your platform uses". It uses at that time the order as selected in the uicontrol via the sorting as selected in the view (I presume that's how you changed the order?).
But, as noted, there's not any place in the OPENFILENAME structure for the info on order selected to be returned by using a wrapper around a native uicontrol call eschewing the Matlab somewhat emasculated wrapper function.

Sign in to comment.

1 Answer

Answer by Image Analyst
on 30 May 2014

If all your files are in the same folder, it's fairly easy. You just use a listbox instead of uigetfile(). By making the selection be a persistent or global variable , and by checking it in the listbox callback to see how it changes with each click , you can use ismember to build up a list of selected items in the order they clicked. Hopefully that's a good enough explanation for you to do it, because I didn't have time to program it up for you.
If they're in different folders, you can still do it with extra case to keep track of which folder each item in your listbox came from but since your current method uses uigetfile() I don't think different folders is the case with you.

  1 Comment

Thanks for answering. I will try using the listbox, but I will certainly need some help. Will let you know once I am on it.

Sign in to comment.