如何使用uigetfile和importdata在matlab中导入多个文件? [英] How to import multiple files in matlab, using uigetfile and importdata?
问题描述
我想选择多个文件,从文件中导入数据并在GUI程序中使用它们。
我用来获取多个文件的代码可以很好地运行:
I want to select multiple files, import the data from the files and use them in GUI program.
The code I am using to get the multiple files works perectly well:
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','Study Files','MultiSelect','on')
Cols = size(FileName,2);
numfiles = Cols;
for i = 1:numfiles
FileName(i)
entirefile =fullfile(PathName,FileName(i))
end
我的问题是当我尝试打开整个文件
。我尝试使用的方法适用于单个文件,但不适用于此。当循环中的代码为:
My problem is when I try to open entire file
. The method I'm trying to use works with a single file but not here.When the code in the loop is:
for i = 1:numfiles
FileName(i)
entirefile =fullfile(PathName,FileName(i))
A = [];
fid = fopen(entirefile);
tline = fgets(fid);
while ischar(tline)
parts = textscan(tline, '%f;');
if numel(parts{1}) > 0
A = [ A ; parts{:}' ];
end
tline = fgets(fid);
end
end
使用fopen时出错第一个输入必须是char类型的文件名,或者类型为double的
文件标识符。
Error using fopen First input must be a file name of type char, or a file identifier of type double.
多选中的错误(第14行)fid = fopen(整个文件);
Error in multiselect (line 14) fid = fopen(entirefile);
<它还只给我选择了第一个和最后一个文件,然后只给出了第一个选定文件的整个文件
。
有关我如何解决此问题的任何建议?
Anyone any suggestions on how I could resolve this issue?
推荐答案
问题是如何访问单元格数组的元素 FileName
。如果使用常规括号访问它,则输出将是单元素单元格数组,因此 fullfile
也将输出单元格数组。您需要使用大括号来访问它,例如 FileName {i}
。
The issue is how you access the element of the cell array FileName
. If you access it with regular brackets, the output will be a one-element cell array, and fullfile
will therefore output a cell array as well. You need to access it with curly brackets like this FileName{i}
.
这应该有效:
[FileName,PathName,FilterIndex] = uigetfile('*.txt*','MultiSelect','on');
numfiles = size(FileName,2);
for ii = 1:numfiles
FileName{ii}
entirefile =fullfile(PathName,FileName{ii})
fid = fopen(entirefile);
% your code
fclose(fid);
end
这篇关于如何使用uigetfile和importdata在matlab中导入多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!