答案 0 :(得分:2)
首先,您需要将file_input
和pathname
组合在一起,以创建所需文件的完整路径。您可以使用函数FULLFILE执行此操作:
dataFile = fullfile(pathname,file_input);
其次,当您使用UIIMPORT时,您可以选择要将文件数据加载到的变量的名称。默认情况下,如果您的文件只包含一种数据(即没有标题文本的数字),则变量名称是您加载的文件的名称,因此如果您不更改存储该变量的变量的名称,则以下内容应该有效文件数据:
uiimport(dataFile); %# Load the data
[filePath,fileName,fileExt] = fileparts(dataFile); %# Get the file name
dataSize = size(eval(fileName)); %# Get the size of the data
disp(dataSize(1)); %# Display the rows
disp(dataSize(2)); %# Display the columns
您也可以使用选项从UIIMPORT输出数据作为数据存储在字段中的结构(文件名作为默认字段名称):
dataStruct = uiimport(dataFile); %# Put the data in a struct
[filePath,fileName,fileExt] = fileparts(dataFile); %# Get the file name
dataSize = size(dataStruct.(fileName)); %# Get the size of the data
disp(dataSize(1)); %# Display the rows
disp(dataSize(2)); %# Display the columns
如果您需要将加载的数据传递给其他函数,或以其他任何方式使用它,您可以执行以下操作:
some_other_fcn(eval(fileName)); %# When loaded with UIIMPORT to a variable
some_other_fcn(dataStruct.(fileName)); %# When loaded with UIIMPORT to a struct
答案 1 :(得分:1)
我会评论您的代码,以便您可以看到发生了什么。
此外,我建议添加(用于调试目的)另外两个disps,以便您了解正在发生的事情。
%# uigetfile reads the name of a file and stores it in file_input, for example 'mydata.dat'
[file_input,pathname] = uigetfile( ...
{'*.txt', 'Text (*.txt)'; ...
'*.xls', 'Excel (*.xls)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select files');
disp(file_input)
%# fileparts splits file_input into name and extension. pathstr is empty, name is 'mydata',
%# ext is '.dat', and versn is empty
[pathstr, name, ext, versn] = fileparts(file_input)
disp(name)
%# name is a string containing, in our example, 'mydata'
%# r is the number of rows in the string 'mydata', which is 1
%# c is the number of columns in the string 'mydata', which is 6
r = size(name,1);
c = size(name,2);
disp(r)
disp(c)
如果您想要数据集的大小,则需要先加载数据集。
或者,如果您的数据集始终具有固定数量的列,例如,您可以尝试根据文件大小估计行数
%# get the file size (and other information about the file) using dir
d = dir(fullfile(pathname,file_input));
%# if the header contains, say, 10 bytes, and each row is 8 bytes, you find the number of rows
%# as follows
headerBytes = 10;
rowBytes = 8;
nRows = (d.size-headerBytes)/rowBytes;
答案 2 :(得分:0)