How to convert .mat file to .csv file without corrupting the data?

9 views (last 30 days)
Hi,
I have a matrix of 1920x1631 without a single null cell. When I use
writematrix(MatrixName,'FileName.csv')
it gives me a csv file. However, when I examine the file, I noticed my data was corrupted. For instance, the first row contains only 20 column instead of 1631 which is the column number of the original matrix. Do you have any suggestion? Is it possible the problem occurs due to my values on the matrix (values are very small and some of them are negative)? Thank you in advance.
  7 Comments
Cris LaPierre
Cris LaPierre on 11 Jan 2022
Edited: Cris LaPierre on 11 Jan 2022
I just used your mat file and code, and the csv file had the correct number of columns. I also used repmat to expand your shared variable to have size 1920x1820. When opening in Excel, it also had 1820 columns. However, I'm using desktop. I tested in R2022a prerelease. What version of MATLAB are you using?
You mention having to dowload the csv file. Are you using MATLAB Online? How are you downloading it?
I did run the same tests in MATLAB Online (current version is R2021b), and then downloaded the csv file. It had 1820 columns, too.
Are there any more details you can provide? Are you omitting any code?
One observation - you second screenshot is showing a mix of periods and commas. It looks like periods are your decimal separator when there is no scientific notation (E05, for example) and a comma when there is. I've included a couple examples of each below.
What do you see if you open the csv file in a text editor like notepad instead of Excel?
Jan
Jan on 11 Jan 2022
@Abdullah Özkan: Do I understand correctly, that the actual problem is, that Excel does not import the CSV file correctly?
The actual data cannot cause the problem, so does this reproduce the error on your computer:
Matrix = rand(1920, 1631);
writematrix(Matrix,'FileName.csv');
By the way: "matrices" is the plural of "matrix".

Sign in to comment.

Accepted Answer

Abdullah Özkan
Abdullah Özkan on 14 Jan 2022
Hi Everyone,
First of all, I never knew the community was this great. Even sometimes, I wished you would answer my questions late; since I couldn't response your answers at your speed.
I solved my problem by converting my .mat file into tab separated .txt file. After, I opened the txt file in Excel, I saved it as .csv file. But still, to clarify the problem for further researchers, I will answer your questions. Thank you for kind help.
The code I used:
dlmwrite('myFile.txt', myFile, 'delimiter','\t','newline','pc')
  • Are you using MATLAB Online?
Yes, I am using the online version from https://matlab.mathworks.com/
  • Are there any more details you can provide? Are you omitting any code?
I took cwt of a wav file with some filtering. Then I concatenated some small blocks from the result and created the matrix.
  • What do you see if you open the csv file in a text editor like notepad instead of Excel?
It is even worse, I guess. I am adding a screenshot.
  • Do I understand correctly, that the actual problem is, that Excel does not import the CSV file correctly?
Yes, you are right. I will use this csv file with python later for machine learning. If there is some other way, I'll be happy to learn.
  • Does this reproduce the error on your computer?
Do you mean, I should download the matlab to my local and try again?
  • By the way: "matrices" is the plural of "matrix".
I knew there was something about this word. Thank you very much. I am editing the incorrect usage.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!