如何在应用程序设计中使用MATLAB的uipickfiles函数?

huangapple go评论60阅读模式
英文:

How do I use MATLABs uipickfiles function in app designer?

问题

我正在尝试(不好地)用MATLAB应用程序设计者编写一个应用程序,该应用程序可以读取包含不同数量文本文件的多个文件夹。

我偶然发现了uipickfiles,并想知道是否有人有使用经验?目前,我的应用程序有一个ButtonPushed回调,该回调调用uipickfiles,然后允许用户选择要使用的文件夹。然而,当我查看返回的内容时,如果只选择了一个文件夹,它会是一个1x1单元格,其中包含文件夹的路径。

我想知道是否可能操纵这个,以创建一个回调,该回调还会读取文件夹中所有文本文件的内容,并将其存储为mxn表格或矩阵?

我尝试过在文件夹上使用不同变体的readtable或readmatrix,即使只有一个文件,也没有成功。我一直收到一个错误 - "filename"必须是字符串标量或字符向量。但我不知道如何让应用程序做我想要的事情。

任何帮助都将不胜感激

英文:

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

答案1

得分: 0

uipickfile只会为您完成部分工作。它将返回您选择的文件夹(不包括它们的内容)。

您可以添加一些额外的代码,使用通配符匹配来获取文件夹的内容,例如只获取txt文件

folders = uipickfiles('num',[]);  % 获取无限数量的文件夹选择
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
英文:

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

huangapple
  • 本文由 发表于 2023年7月10日 22:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定