如何将 SPSS 数据文件直接传输到 Matlab 矩阵,反之亦然?
How to transfer SPSS data file directly to a Matlab matrix and vice versa?
有谁知道在 Matlab 矩阵中传输 SPSS .sav 或任何其他 SPSS 数据文件的最佳方法是什么,反之亦然? (也许直接)
我找到的唯一方法是:
- 已将 SPSS 数据集保存为 Excel 文件。
- 在 Matlab 工作区中打开了 excel 文件。
- 通过以下方式读取 Matlab-matrix (A) 中的 excel 文件:
A = readxls('filename.xlsx')
我正在寻找在 Matlab 矩阵中 bring/convert SPSS 数字数据集的直接方法,反之亦然。
从 Matlab 到 SPSS 我已经将我的 Matlab 矩阵导出到一个 .txt 文件中并在 SPSS 中打开它。
如果 Matlab 支持 ODBC 输入并且您可以获得 SPSS ODBC 驱动程序,则可以直接针对 SAV 文件发出 ODBC 查询。
我找到了 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
or by using the import tool:
将其导入 Matlab
uiimport(your_resulted_CSV_file_with_full_path.csv)
此工具可让您导入混合数据,并生成脚本以便下次自动完成。
对于相反的方向(Matlab 到 SPSS),请查看这些提交给 File Exchange 的文件:
*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)
。
有谁知道在 Matlab 矩阵中传输 SPSS .sav 或任何其他 SPSS 数据文件的最佳方法是什么,反之亦然? (也许直接)
我找到的唯一方法是:
- 已将 SPSS 数据集保存为 Excel 文件。
- 在 Matlab 工作区中打开了 excel 文件。
- 通过以下方式读取 Matlab-matrix (A) 中的 excel 文件:
A = readxls('filename.xlsx')
我正在寻找在 Matlab 矩阵中 bring/convert SPSS 数字数据集的直接方法,反之亦然。
从 Matlab 到 SPSS 我已经将我的 Matlab 矩阵导出到一个 .txt 文件中并在 SPSS 中打开它。
如果 Matlab 支持 ODBC 输入并且您可以获得 SPSS ODBC 驱动程序,则可以直接针对 SAV 文件发出 ODBC 查询。
我找到了 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
or by using the import tool:
uiimport(your_resulted_CSV_file_with_full_path.csv)
此工具可让您导入混合数据,并生成脚本以便下次自动完成。
对于相反的方向(Matlab 到 SPSS),请查看这些提交给 File Exchange 的文件:
*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)
。