Reading a matrix (or array) from an excel file

Hi,
I'm relatively new to matlab, so sorry for very basic questions. I have 3 questions;
1) I want to read a matrix from an excel file and want to assign it a variable named as N_C. I added the following code in my .m file;
N_C=xlsread('C:\Users\Kajal Chopra\vibrations\free_vibrations_program\matlab_input_free_vibrations_1',1);
The values are in sheet 1.I'm also attaching the excel file for your reference. I do not want to tell Matlab that how many rows and columns the matrix has. My matrix has 3 rows and 3 columns. Is it possible that without telling Matlab the number of rows and columns, I carry out the filling up of my matrix/array "N_C" by reading it from xls file using xlsread.
2) Second, I want the end user to enter the xls file name from the command window and my .m file (as in the above location) should then open the file. That is, I do not want to give the file name as:
'C:\Users\Kajal Chopra\vibrations\free_vibrations_program\matlab_input_free_vibrations_1'\
but a user specified file name from the command window.Is it possible?
3) Thirdly, how can I test of my N_C has been correctly assigned after the .m file has read it from xls file?
Thanks a million in advance.
Kajal

3 Kommentare

Star Strider
Star Strider am 26 Jul. 2015
‘I'm also attaching the excel file for your reference.’
Not yet. Give it another go.
kajalschopra
kajalschopra am 26 Jul. 2015
Bearbeitet: kajalschopra am 26 Jul. 2015
Thanks and sorry.
See below. Now attached.
I have attached the xls file. See sheet 1.
0 0 0 1 0 0 2 0 0
The sheet 1 has above values.
I want:
N_C(1,1)= 0 (i.e. the value in A1) N_C(1,2)=0 (i.e. the value in B1) N_C(1,3)=0 (i.e. the value in C1)
N_C(2,1)=1 (i.e. the value in A2) N_C(2,2)=0 (i.e. the value in B2) N_C(2,3)=0 (i.e. the value in C2)
N_C(3,1)=2 (i.e. the value in A3) N_C(3,2)=0 (i.e. the value in B3) N_C(3,3)=0 (i.e. the value in C3)
kajalschopra
kajalschopra am 26 Jul. 2015
Can anyone please help, I need to sort it tonight

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cedric
Cedric am 26 Jul. 2015
Bearbeitet: Cedric am 26 Jul. 2015

0 Stimmen

Hi, try something along the following line:
[filename, filepath] = uigetfile( '*.xls*' ) ;
N_C = xlsread( fullfile( filepath, filename ), 'Sheet1' ) ;
Then, when you say "test if correct", what does it mean? You can test if the size is correct for example:
assert( all( size( N_C ) == 3 ), 'Invalid N_C.' ) ;
if it has to be 3x3, but you'll have to define more precisely which other tests are relevant.

3 Kommentare

Thanks. I have put in the .m file as follows.
N_C=xlsread('C:\Users\KajalChopra\vibrations\free_vibrations_program\matlab_input_free_vibrations_1','Sheet1');
Where /How can I print the contents of N_C to test if it is correct or not?
kajalschopra
kajalschopra am 26 Jul. 2015
Anyone or Cedric..please help.
Cedric
Cedric am 27 Jul. 2015
Bearbeitet: Cedric am 27 Jul. 2015
I provided you the 3 lines of code which ask the user to pick a file, read the file, and check that it is a 3x3 array:
[filename, filepath] = uigetfile( '*.xls*' ) ;
N_C = xlsread( fullfile( filepath, filename ), 'Sheet1' ) ;
assert( all( size( N_C ) == 3 ), 'Invalid N_C.' ) ;
You can display the content of variables by typing their name in the command window, or by looking them up in the "Workspace" panel/window.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 26 Jul. 2015

Bearbeitet:

am 27 Jul. 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by