Get dimensions for dataset matlab, Size Function

2019-07-25 17:10发布

Hello I have been trying to display the dimensions of a dataset, below is the following matlab code that i used...but it gives me the wrong output....i'e number of rows and columns values are wrong....any help regarding this issue would be highly appreciated

[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)

3条回答
时光不老,我们不散
2楼-- · 2019-07-25 17:27

In this case name is the name of the file, which will be a character array. So r will always be 1, and c will be the number of characters in the filename.

EDIT: You probably meant to call readxls (or similar) on the file identified by file_input.

查看更多
成全新的幸福
3楼-- · 2019-07-25 17:29

I'll comment your code so that you can see what is happening where.

Also, I suggest adding (for debugging purposes) two more disps as indicated so you'll see what is going on.

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

If you want the size of your dataset, you need to load the dataset first.

Alternatively, if your dataset has always a fixed number of columns, for example, you can try to estimate the number of rows from the size of the file

%# 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;
查看更多
Fickle 薄情
4楼-- · 2019-07-25 17:39

Firstly, you need to combine file_input and pathname together to create the full path to the file you want. You can do this with the function FULLFILE:

dataFile = fullfile(pathname,file_input);

Secondly, when you use UIIMPORT you can choose the name for the variable that you want the file data to be loaded into. By default, the variable name is the name of the file you loaded if your file contains only one kind of data (i.e. numbers with no header text), so the following should work if you don't change the name of the variable storing the file data:

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

You could also do this using the option to output the data from UIIMPORT as a structure where the data is stored in a field (with the file name as the default field name):

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

If you needed to pass the loaded data to another function, or use it in any other way, you can do the following:

some_other_fcn(eval(fileName));  %# When loaded with UIIMPORT to a variable
some_other_fcn(dataStruct.(fileName));  %# When loaded with UIIMPORT to a struct
查看更多
登录 后发表回答