【问题标题】:Get dimensions for dataset matlab, Size Function获取数据集matlab的尺寸,尺寸函数
【发布时间】:2010-05-04 12:07:32
【问题描述】:

您好,我一直在尝试显示数据集的维度,下面是我使用的以下 matlab 代码……但它给了我错误的输出……即行数和列数的值是错误的....任何有关此问题的帮助将不胜感激

[file_input,pathname]  = uigetfile( ...
    {'*.txt', 'Text (*.txt)'; ...
    '*.xls', 'Excel (*.xls)'; ...
    '*.*', 'All Files (*.*)'}, ...
    'Select files');

    uiimport(file_input)

    [pathstr, name, ext, versn] = fileparts(file_input)


    r = size(name,1);
    c = size(name,2);

    disp(r) 
    disp(c)

【问题讨论】:

    标签: matlab size file-io


    【解决方案1】:

    首先,您需要将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
    

    【讨论】:

      【解决方案2】:

      我会评论你的代码,这样你就可以看到发生了什么。

      另外,我建议添加(出于调试目的)另外两个显示,以便您了解发生了什么。

      %# 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;
      

      【讨论】:

      • 我刚刚编辑了我发给你的matlab代码,我使用uiimport将一个数据集导入到工作区....nw d dataset有一个header和d实际数据,nw matlab保存实际数据和textheaders不同......现在实际数据集默认保存为工作区中的“数据”......并显示真实尺寸......我如何在工作区中显示该变量的尺寸? ???
      • 如果变量的名字是data,你可以使用size(data)得到它的大小
      • 那工作....非常感谢...但我有一些问题要问......首先,当我使用 uigetfile 获取文件并使用 uiimport 导入到工作区.....下一个命令 size(data) 在 uiimport 函数完成执行之前立即运行,这会导致错误,因为尚未创建 d "data" 变量另外,在我的 GUI 中......我想将“数据”变量的 d 内容(这是工作区中已经存在的实际数据集)传递给 d GUI 中指定的另一个函数.....我如何实现 diz?
      【解决方案3】:

      在这种情况下,name 是文件的名称,它将是一个字符数组。所以r 将始终为 1,c 将是文件名中的字符数。

      编辑: 您可能打算在file_input 标识的文件上调用readxls(或类似名称)。

      【讨论】:

      • hello....d 维度 r 实际上是使用函数 uigetfile 后数据集的原始维度...
      • @Tim:我认为你对 uigetfile 的使用有误。该函数仅将文件名和路径作为字符串返回,而不是文件的内容。
      • 我刚刚编辑了我发给你的matlab代码,我使用uiimport将一个数据集导入到工作区....nw d dataset有一个header和d实际数据,nw matlab保存实际数据和textheaders不同......现在实际数据集默认保存为工作区中的“数据”......并显示真实尺寸......我如何在工作区中显示该变量的尺寸? ???
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2014-12-14
      相关资源
      最近更新 更多