2017-06-17 592 views
2

有誰知道什麼是在Matlab矩陣中傳輸SPSS .sav或任何其他SPSS數據文件的最佳方法,反之亦然? (也許直接)如何將SPSS數據文件直接傳輸到Matlab矩陣,反之亦然?

我發現的唯一辦法是這樣的:

  1. 保存的SPSS數據集作爲一個Excel文件。
  2. 打開Matlab工作區中的excel文件。
  3. 通過閱讀在Matlab矩陣(A)的Excel文件:A = readxls('filename.xlsx')

我正在尋找一種直接的方式把/轉換SPSS數值數據集中在Matlab的矩陣而減少,反之亦然。

從Matlab到SPSS我已經將我的Matlab矩陣導出到一個.txt文件中,並打開它SPSS。

回答

1

我找到了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

開始通過編寫腳本文件SPSS(以下是PSPP,但應該是非常相似的SPSS)。

保存這個文件(假設它叫做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

* PSPP是免費的,作品非常快,因此,如果您不能執行此在SPSS只要下載。


這裏是你可以在Matlab如果你安裝了PSPP,沒有走出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)

0

如果Matlab支持ODBC輸入並且可以獲得SPSS ODBC驅動程序,則可以直接對SAV文件發出ODBC查詢。

+0

Matlab確實[支持ODBC](https://www.mathworks.com/help/database/ug/importing-data-from-databases-into-matlab.html),但OP將需要[數據庫工具箱] (https://www.mathworks.com/help/database/index.html)。另外,我不確定此選項是否在SPSS的核心許可下打開。 – EBH

+0

我認爲驅動程序是免費的,可能包含在基本產品許可的數據訪問包中。 – JKP

相關問題