简体   繁体   English

将.mat格式的数据集转换为.csv octave / matlab

[英]Convert dataset of .mat format to .csv octave/matlab

there are datasets in .mat format in the this site: http://www.cs.nyu.edu/~roweis/data.html 本网站有.mat格式的数据集: http//www.cs.nyu.edu/~roweis/data.html
I want to change the format to .csv. 我想将格式更改为.csv。 Can someone tell me how to change the format to create the .csv file. 有人可以告诉我如何更改格式以创建.csv文件。

Thanks! 谢谢!

Suppose that the .mat files from the site are available already. 假设站点中的.mat文件已经可用。 In the command window in Matlab, you may write, for example: 在Matlab的命令窗口中,您可以编写,例如:

load('C:\Users\YourUserName\Downloads\mnist_all.mat');

to load the .mat file; 加载.mat文件; the result should be a set of matrices test0 , test1 , ..., train0 , train1 ... created in your workspace, which you want saved as CSV files. 结果应该是在工作区中创建的一组矩阵test0test1 ,..., train0train1 ...,您希望将其另存为CSV文件。 Because they're different size, you need to save one CSV per variable, eg (also in the command window): 因为它们的大小不同,所以每个变量需要保存一个CSV,例如(也在命令窗口中):

csvwrite('C:\Users\YourUserName\Downloads\mnist_test0.csv', test0);

Repeat the command for each variable, and do not forget to change also the name of the output file to avoid overwriting. 对每个变量重复该命令,并且不要忘记更改输出文件的名称以避免覆盖。

Did you tried the csvwrite function in Matlab? 你在Matlab中尝试过csvwrite函数吗?

Just load your .mat files with the load function and then write them with csvwrite ! 只需使用load函数加载.mat文件,然后使用csvwrite写入它们!

I do not have a Matlab license so I installed GNU Octave 4.2.1 (2017) on Windows 10 (thank you to John W. Eaton and others). 我没有Matlab许可证所以我在Windows 10上安装了GNU Octave 4.2.1(2017)(感谢John W. Eaton和其他人)。 I was not fully successful using the csvwrite so I used the following workaround. 我没有完全成功使用csvwrite所以我使用了以下解决方法。 (BTW, I am totally incompetent in the Octave world. csvwrite worked for simple data structures). (顺便说一句,我在Octave世界中完全不称职.csvwrite适用于简单的数据结构)。

In the Command Window I used the following two commands 在命令窗口中,我使用了以下两个命令

load myfile.mat 加载myfile.mat

save("-text","myfile.txt","variablename") 保存( “ - 文本”, “myfile.txt的”, “VARIABLENAME”)

When the "myfile.mat" is loaded, the variable names for the data vectors loaded are displayed in the workspace window. 加载“myfile.mat”时,加载的数据向量的变量名称将显示在工作区窗口中。 This is the name(s) to use in the save command. 这是在save命令中使用的名称。 Some .mat files will load several data structures. 一些.mat文件将加载多个数据结构。

The "-text" option is the default, so you may not need to include this option in the command. “-text”选项是默认选项,因此您可能不需要在命令中包含此选项。

The output file lists the .mat file contents in text format as single column (of potentially sequential variables). 输出文件将.mat文件内容以文本格式列为单列(可能是顺序变量)。 It should be easy to use you text editor to massage this data into the original matrix structure for use in whatever app you are comfortable with. 应该很容易使用文本编辑器将这些数据按到原始矩阵结构中,以便在您熟悉的任何应用程序中使用。

Had a similar issue. 有一个类似的问题。 Needed to convert a series of .mat files that had two columns of numerical data into standard data files (ascii text). 需要将具有两列数值数据的一系列.mat文件转换为标准数据文件(ascii文本)。 Note that I don't really ever use csv, but everything here could be adapted by using csvwrite instead of the standard save. 请注意,我并不真正使用csv,但这里的所有内容都可以通过使用csvwrite而不是标准保存进行调整。

Using Octave 4.2.1 .... 使用Octave 4.2.1 ....

load myfile.mat  
LI = [L, I]          ## L and I are column vectors representing my data     
save myfile.txt LI

Note that L and I appear to be default variable names chosen by Octave for the two columns vectors in my original data file. 请注意,L和I似乎是Octave为原始数据文件中的两个列向量选择的默认变量名。 Ideally a script that iterated over all files with the .mat extension in my directory would be ideal, but this got the job done. 理想情况下,迭代在我的目录中具有.mat扩展名的所有文件的脚本将是理想的,但这完成了工作。 It saves the data as two space separated columns of data. 它将数据保存为两个空格分隔的数据列。

*** Update ***更新

The following script works on Octave 4.2.1 for a series of data files with the .mat extension that are in the same directory. 以下脚本适用于Octave 4.2.1,用于一系列具有.mat扩展名的数据文件,这些文件位于同一目录中。 It will iterate over them and write the data out to text files with the same name but with the extension .dat . 它将迭代它们并将数据写入具有相同名称但扩展名为.dat的文本文件中。 Note that this is not efficient, so if you have a lot of files or if they are large it can take a while to run. 请注意,这不是很有效,因此如果您有大量文件或者它们很大,则可能需要一段时间才能运行。 I would suggest that you run it from the command line using octave mat2dat.m so you can actually watch it go. 我建议您使用octave mat2dat.m从命令行运行它,这样您就可以实际观看它了。

I make no guarantees that this will work for you, but it did for me. 我不保证这对你有用,但它确实适合我。 I also am NOT proficient in Octave or Matlab, so I'm sure a better solution exists. 我也不精通Octave或Matlab,所以我相信存在更好的解决方案。

# mat2dat.m

dirlist = glob("*.mat")
for i=1:length(dirlist)
  filename = dirlist{i,1}
  load(filename, "L", "I")
  LI = [L,I]
  tmpname = filename(1:length(filename)-3)
  txtname = strcat(tmpname, 'dat')
  save(txtname, "LI")
end

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

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