简体   繁体   English

将.mat转换为.csv octave / matlab

[英]Convert .mat to .csv octave/matlab

I'm trying to write an octave program that will convert a .mat file to a .csv file. 我正在尝试编写一个将.mat文件转换为.csv文件的八度程序。 The .mat file has a matrix X and a column vector y. .mat文件具有矩阵X和列向量y。 X is populated with 0s and 1s and y is populated with labels from 1 to 10. I want to take y and put it in front of X and write it as a .csv file. X填充0和1,y填充1到10的标签。我想把y放在X前面并将其写为.csv文件。

Here is a code snippet of my first approach: 这是我的第一种方法的代码片段:

load(filename, "X", "y");
z = [y X];

basename = split{1};
csvname = strcat(basename, ".csv");

csvwrite(csvname, z);

The resulting file contains lots of really small decimal numbers, eg 8.560596795891285e-06,1.940359477121703e-06, etc... 生成的文件包含许多非常小的十进制数,例如8.560596795891285e-06,1.940359477121703e-06等...

My second approach was to loop through and manually write the values out to the .csv file: 我的第二种方法是循环并手动将值写入.csv文件:

load(filename, "X", "y");
z = [y X];

basename = split{1};
csvname = strcat(basename, ".csv");
csvfile = fopen(csvname, "w");

numrows = size(z, 1);
numcols = size(z, 2);

for i = 1:numrows
  for j = 1:numcols
    fprintf(csvfile, "%d", z(i, j));
    if j == numcols
      fprintf(csvfile, "\n");
    else
      fprintf(csvfile, ",");
    end
  end
end

fclose(csvfile);

That gave me a correct result, but took a really long time. 这给了我一个正确的结果,但花了很长时间。

Can someone tell me either how to use csvwrite in a way that will write the correct values, or how to more efficiently manually create the .csv file. 有人能告诉我如何以一种能够写出正确值的方式使用csvwrite,或者如何更有效地手动创建.csv文件。

Thanks! 谢谢!

The problem is that if y is of type char , your X vector gets converted to char, too. 问题是如果ychar类型,你的X向量也会被转换为char。 Since your labels are nothing else but numbers, you can simply convert them to numbers and save the data using csvwrite : 由于您的标签只是数字,您只需将它们转换为数字并使用csvwrite保存数据:

csvwrite('data.txt', [str2num(y) X]);

Edit Also, in the loop you save the numbers using integer conversion %d , while csvwrite writes doubles if your data is of type double . 编辑此外,在循环中使用整数转换%d保存数字,而如果数据类型为double ,则csvwrite写入双精度数。 If the zeros are not exactly zeros, csvwrite will write them with scientific notation, while your loop will round them. 如果零不完全为零,则csvwrite将使用科学记数法编写它们,而您的循环将围绕它们。 Hence the different behavior. 因此,不同的行为。

Just a heads up your code isn't optimized for Matab / octave. 只是抬起你的代码并没有针对Matab / octave进行优化。 Switch the for i and for j lines around. 切换for i和j行。

Octave is in column major order so its not cache efficient to do what your doing. Octave是列的主要顺序,因此它不会缓存有效地执行您的操作。 It will speed up the overall loop by making the change to probably an acceptable time 通过将更改改为可接受的时间,它将加速整个循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM