1

I am attempting (poorly) to write an app in MATLAB app designer that can read in several folders containing a variable number of text files.

I stumbled upon uipickfiles and was wondering if anyone has any experience using it? Currently my app has a ButtonPushed callback that calls uipickfiles which then allows the user to choose the folders they wish to use. However, when I look at what is returned it is e.g. - if only one folder is selected - a 1x1 cell containing the path to the folder.

I am wondering if it is possible to manipulate this to create a callback that also reads the contents of all the textfiles in the folder and stores it as a mxn table or matrix?

I've tried using different variations of readtable or readmatrix on a folder with even one file in it to no avail. I keep getting an error - "filename" must be a string scalar or character vector. But I don't know how to make the app do what I want.

Any help at all would be greatly appreciated

Chaz69631
  • 15
  • 6
  • `uipickfiles' isn't a standard MATLAB function, it is something that is available from the File Exchange though. Do you mean this function https://uk.mathworks.com/matlabcentral/fileexchange/10867-uipickfiles-uigetfile-on-steroids – scotty3785 Jul 10 '23 at 14:11
  • You don’t need to change the `uipickfiles` function, you just need to list the files in the directory and read them in one by one. There are many answers on this site showing how to do this. Google for something like “matlab read all files in directory”. – Cris Luengo Jul 10 '23 at 14:32

1 Answers1

0

uipickfile will only do part of this for you. It will return the folders that you've selected (not their contents).

You can add some additional code to get the folder contents using wildcard matching to just get txt files for example

folders = uipickfiles('num',[]);  %Get an unlimited number of folder selections
filter = '\*.slx';

file_contents = struct;

for i=1:length(folders)
   current_folder = folders{i};
   text_files_contained = dir([current_folder filter]);
   file_contents(i).folder_path = current_folder;
   file_contents(i).files = {text_files_contained.name};
end
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • Hi Scotty, thanks for the reply. I can see the variable text_files_contained created by the for loop as a Nx1 struct, containing the file name, the folder path, the date it was created, the amount of bytes it contains, an isdir logical and datenum. But how do I tell the code to loop through the folder and create a new table or matrix of values for each file based on how many text files/folders were selected in uipickfiles? Sorry if this is basic, I'm only newly learning Matlab – Chaz69631 Jul 11 '23 at 06:47
  • The file_contents variable is a structure that contains a different element for each folder where the directory name is in `folder_path` and the list of files within that folder is within `files`. This is a "friendlier" data format to use rather than a matrix of values. – scotty3785 Jul 11 '23 at 08:05
  • matrices don't work well with char arrays so you end up having to use cell arrays instead. At which point you may as well use a more powerful data type like structs. – scotty3785 Jul 11 '23 at 08:06
  • I've tinkered around with the code you supplied and my own. By using uigetdir I can make 2 structs, one containing what text_files_contained has and one containing the file data and the colheaders from the txt file. Is it possible to make uipickfiles do this as when I expand text_files_contained I can't actually see the data? My code uses importdata to make the other struct containing the values I need is it possible to do this with yours as ideally I'd like to use uipickfiles for ease of use for the end user – Chaz69631 Jul 11 '23 at 10:52
  • Yes it's possible. Personally I'd be creating a table of each imported data file and placing that as a child of `file_contents` like `file_contents(i).data(fIdx) = imported_data`. `imported_data' is a table that is the result of using a function to import data from a specific file and `fIdx` is the index of the current file that you are reading. – scotty3785 Jul 11 '23 at 12:51