MATLAB Converting Excel Spreadsheet to Matrix and Storing in Mfile

  • Thread starter Thread starter elbarto
  • Start date Start date
  • Tags Tags
    Excel Matrix
AI Thread Summary
To convert a matrix imported from an Excel spreadsheet into a specific format in MATLAB, users can utilize the fprintf function alongside num2str to print each row in the desired format. To store the matrix for future use without relying on the original spreadsheet, the save function can be employed to create an M-file. This allows the matrix data to be accessed across different machines without needing to re-import from Excel. The discussion emphasizes the importance of maintaining data accessibility and ease of use in MATLAB. Overall, these methods facilitate efficient data management in MATLAB environments.
elbarto
Messages
32
Reaction score
0
Hi
I have just imported a fairly large matrix from a excel spread sheet (121x6) useing the xlsread function. Is there a way I can convert the matrix from my command window to look something like below.

Matrix = [...; ...; ...; ...; ...; ...]

I want to know if i can store the matrix in a mfile so I can use the data even if i no longer have access to the spreadsheet. I don't really want to use xlsread everytime i need to use the matrix and I want to be able to run the code on different machines if i need.

Thanks in advance

Elbarto
 
Physics news on Phys.org
elbarto said:
Is there a way I can convert the matrix from my command window to look something like below.

Matrix = [...; ...; ...; ...; ...; ...]
It can be done in this way (your array is arr)
Code:
[m n] = size(arr);
for i = 1:m
fprintf([num2str(arr(i,:)) ';'])
end
The fprintf prints everything in the same line. num2str converts integer to string. arr(i,:) extracts each row of the matrix. Following are some references:
https://in.mathworks.com/matlabcentral/answers/116593-how-to-display-a-string-and-matrix-in-matlabhttps://in.mathworks.com/matlabcentral/answers/175231-extract-first-row-from-matrixhttps://in.mathworks.com/matlabcentral/answers/16963-how-to-print-in-the-same-line
elbarto said:
want to know if i can store the matrix in a mfile so I can use the data even if i no longer have access to the spreadsheet. I don't really want to use xlsread everytime i need to use the matrix and I want to be able to run the code on different machines if i need.
This can be done using the save function:
https://in.mathworks.com/help/matlab/ref/save.html
 

Similar threads

Replies
18
Views
6K
Replies
4
Views
4K
Replies
4
Views
6K
Replies
32
Views
4K
Replies
3
Views
3K
Back
Top