Converting Excel Spreadsheet to Matrix and Storing in Mfile

  • Context: MATLAB 
  • Thread starter Thread starter elbarto
  • Start date Start date
  • Tags Tags
    Excel Matrix
Click For Summary
SUMMARY

This discussion focuses on converting an Excel spreadsheet into a MATLAB matrix and storing it in an M-file for future use. The user imported a 121x6 matrix using the xlsread function and sought a method to convert and store this matrix without needing to access the original spreadsheet repeatedly. The solution involves using the fprintf function to format the matrix and the save function to store it in an M-file, ensuring accessibility across different machines.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of matrix operations in MATLAB
  • Knowledge of the xlsread function for importing Excel data
  • Basic understanding of file handling in MATLAB using the save function
NEXT STEPS
  • Learn how to use the fprintf function for custom output formatting in MATLAB
  • Explore the save function in MATLAB for storing variables in .mat files
  • Investigate methods for loading data from M-files using the load function
  • Research best practices for managing large datasets in MATLAB
USEFUL FOR

MATLAB users, data analysts, and engineers who need to efficiently manage and store matrix data for reproducibility and accessibility across different computing 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 ·
Replies
18
Views
6K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K