无法在Matlab中使用uigetfile打开文件 [英] Unable to open a file with uigetfile in Matlab

查看:424
本文介绍了无法在Matlab中使用uigetfile打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个允许用户打开某些文件的代码.

I am building a code that lets the user open some files.

reference = warndlg('Choose the files for analysis.');
uiwait(reference);
filenames2 = uigetfile('./*.txt','MultiSelect', 'on');
if ~iscell(filenames2)
    filenames2 = {filenames2};    % force it to be a cell array of strings
end
numberOfFiles = numel(filenames2);

data = importdata(filenames2{i},delimiterIn,headerlinesIn);

运行代码时,出现提示,按OK,然后什么也没有发生.代码停止了,告诉我:

When I run the code, the prompts show up, I press OK, and then nothing happens. The code just stops, telling me :

Error using importdata (line 137)
Unable to open file.

Error in FreqVSChampB_no_spec (line 119)
data=importdata(filenames2{1},delimiterIn,headerlinesIn);

我只是没有机会选择一个文件.细胞阵列保持为空,如下图所示.

I just don't have the opportunity to select a file. The cellarray stays empty as showed in the following image.

推荐答案

MATLAB找不到您选择的文件.您的变量filenames2仅包含文件名,而不包含其完整路径.如果您没有提供importdata的完整路径,它将搜索您在MATLAB路径上提供的任何文件名,如果找不到它,则会出现错误,如您所见.

MATLAB can't find the file that you have selected. Your variable filenames2 contains only the name of the file, not its full path. If you don't provide the full path to importdata, it will search for whatever file name you provide on the MATLAB path, and if it can't find it it will error as you see.

尝试类似的事情-为了便于描述,我只是通过单选来做,但是您可以通过多选来做类似的事情.

Try something like this - I'm just doing it with single selection for ease of description, but you can do something similar with multiple selection.

[fileName, pathName] = uigetfile('*.txt');
fullNameWithPath = fullfile(pathName, fileName);
importdata(fullNameWithPath)

fullfile很有用,因为它会在pathNamefileName之间插入正确的字符(在Windows中为\,在Unix中为/).

fullfile is useful, as it inserts the correct character between pathName and fileName (\ on Windows, / on Unix).

这篇关于无法在Matlab中使用uigetfile打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆