views:

230

answers:

3

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)
A: 

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.

Richie Cotton
hello....d dimensions r actually the original dimensions of the dataset after using the function uigetfile...
Tim
@Tim: I think you are mistaken about the use of uigetfile. That function only returns the filename and path as strings, not the contents of the file.
Doresoom
I just edited the matlab code I sent you, I used uiimport to import a dataset into the workspace....nw d dataset had a header and d actual data,nw matlab saves actual data and textheaders differently......now the actual dataset's is saved as "data" in the workspace by default....and shows the true dimension....how do i get to display he dimension of that variable in the workspace????
Tim
+1  A: 

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;
Jonas
I just edited the matlab code I sent you, I used uiimport to import a dataset into the workspace....nw d dataset had a header and d actual data,nw matlab saves actual data and textheaders differently......now the actual dataset's is saved as "data" in the workspace by default....and shows the true dimension....how do i get to display he dimension of that variable in the workspace????
Tim
If the name of the variable is `data`, you can get its size using `size(data)`
Jonas
that worked....thanks a whole lot...but I have some questions to ask......Firstly when I use the uigetfile to get the file and use uiimport to import to the workspace.....the next command which is size(data) runs immediately before d uiimport function finishes executing which leads to an error coz d "data" variable has not yet been createdAlso,in my GUI....I would like to pass d contents of the "data"variable (which is the actual data set already in the workspace) to another function specified in d GUI.....how do i achieve diz?
Tim
+2  A: 

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
gnovice