如何将SPSS数据文件直接传输到Matlab矩阵,反之亦然?

时间:2017-06-17 22:26:49

标签: matlab matrix statistics spss

有谁知道在Matlab矩阵中传输SPSS .sav或任何其他SPSS Data文件的最佳方法是什么,反之亦然? (也许直接)

我发现的唯一方法是:

  1. 将SPSS数据集保存为Excel文件。
  2. 在Matlab工作区中打开excel文件。
  3. 通过:A = readxls('filename.xlsx')
  4. 在Matlab-matrix(A)中读取excel文件

    我正在寻找一种在Matlab矩阵中引入/转换SPSS数值数据集的直接方法,而不是。

    从Matlab到SPSS我已将我的Matlab矩阵导出到.txt文件中并打开它SPSS。

2 个答案:

答案 0 :(得分:1)

我找到了PSPP *的解决方案,这对SPSS也有好处。这个想法很简单 - 按照你在问题中描述的内容,但是以自动方式。

首先为SPSS编写一个脚本文件(以下内容适用于PSPP,但应与SPSS非常相似):

GET FILE =  'your_SPSS_file_with_full_path.sav'
save translate
        /outfile = 'your_resulted_CSV_file_with_full_path.csv'
        /type = CSV
        /REPLACE
        /FIELDNAMES
        /CELLS=LABELS.
如果你想要数字而不是标签,请

CELLS=VALUES

保存此文件(假设它叫做spss2csv.sps)。

在Matlab中,写下以下内容:

sps_file_name = 'full_path\spss2csv.sps';
% replace the path below with the path for SPSS:
command = ['"C:\Program Files\PSPP\bin\pspp.exe" ' sps_file_name];
system(command)

这将生成一个.csv文件,您可以通过xlsread或使用import tool导入Matlab:

uiimport(your_resulted_CSV_file_with_full_path.csv)

此工具允许您导入混合数据,并生成脚本,以便下次可以自动完成。

对于相反的方向(Matlab到SPSS),请查看文件交换的这些提交:

  1. export data to spss
  2. save4spss
  3. * PSPP是免费的,并且工作速度非常快,所以如果你不能在SPSS中实现它,只需下载它。

    如果您安装PSPP,可以在Matlab中使用这个函数,而不必离开Matlab:

    function import_sav(sav_file,labels)
    % Opens the import tool for importins a sav file
    % 'sav_file' is the name of the SPSS\PSPP file to import
    % 'labels' is a logical that if true then the file is imported using the
    % labels and not the numeric values. Default is TRUE.
    
    if nargin<2
        labels = true;
    end
    [p,csv_file] = fileparts(sav_file);
    if isempty(p), p = cd; end
    sps_file = [p '\convert2csv.sps'];
    % sav_file = 'C:\Users\Eyal\Dropbox\MATLAB\atid\result.sav';
    csv_file = [p '\' csv_file '.csv'];
    fid = fopen(sps_file,'w');
    fprintf(fid,'GET FILE =  ''%s''\n',sav_file);
    fprintf(fid,'save translate\n');
    fprintf(fid,'\t\t/outfile = ''%s''\n',csv_file);
    fprintf(fid,'\t\t/type = CSV\n');
    fprintf(fid,'\t\t/REPLACE\n');
    fprintf(fid,'\t\t/FIELDNAMES\n');
    if labels
        fprintf(fid,'\t\t/CELLS=LABELS.\n');
    else
        fprintf(fid,'\t\t/CELLS=VALUES.\n');
    end
    fclose(fid);
    command = ['"C:\Program Files\PSPP\bin\pspp.exe" ' sps_file];
    system(command)
    uiimport(csv_file)
    end
    

    这将自动打开您在调用函数时输入的.sav文件的数据导入工具:import_sav('my_file.sav',1)

答案 1 :(得分:0)

如果Matlab支持ODBC输入并且您可以获得SPSS ODBC驱动程序,则可以直接针对SAV文件发出ODBC查询。

相关问题