九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
matlab讀取excel文件及其數(shù)據(jù)處理

轉自 http://blog.chinaunix.net/uid-25067956-id-1743352.html


在許多時候我們要借助于matlab讀取excel的內容進行處理,以下是一種常用的處理方法

       office的表格文件也就是xls文件本質上就是一個二維矩陣,二維矩陣是用來保存數(shù)據(jù)的最佳方式,所以在日常工作中,我們從其它地方獲取的數(shù)據(jù)通常都被保存為xls格式,但處理數(shù)據(jù)時,我們卻需要把xls文件的數(shù)據(jù)導入到matlab里進行處理。

      如果你只處理一個文件并且只做一次的話,你可以手動來拷貝粘貼,這花費不了你太多時間。如果有很多xls文件,或者你的xls文件的內容可能隨時被修改,那么下面的方法可以派上用場。

      matlab自身提供了大量的函數(shù),包括讀取office文件。其中xlsreadxlswrite就是專門用來讀取xls文件里的數(shù)據(jù)的。這兩個函數(shù)的使用方法可以直接查看matlab自帶的幫助。

      xlsread對于純數(shù)據(jù)的xls文件支持很完美,也就是說當xls文件里的每個格子都是“數(shù)”時,xlsread會直接返回一個實數(shù)矩陣。但是通常我們拿到xls文件并不是這樣,它的表頭多半是描述性文字,它的數(shù)據(jù)也有可能是文字,有些位置的數(shù)據(jù)還有可能是缺失的。xlsread對這樣的文件讀取無能為力,或者說需要大量的時間去協(xié)調數(shù)據(jù)的位置信息。要是有一個函數(shù),能夠按照原有的順序直接讀取所有的單位格數(shù)據(jù)就好了。當然,這時候返回的矩陣就不能是一個數(shù)值矩陣了,它將會是一個cell矩陣,里面的每個元素類型可能不一樣。

       matlab本身并不提供這個功能,但是另外有一個函數(shù)officedoc完美的實現(xiàn)這個功能。這個函數(shù)包可以去OfficeDoc官方網站上去下載,解壓縮后放到工作路徑上即可。使用方法可以查詢help officedoc。officedoc是收費函數(shù)包,但有免費版本,而且其免費版本可以實現(xiàn)上面我們所說的效果(收費版本主要是可以用來修改office文件)。

      例子:

      matlab中讀取xls格式的文件內容如應用如下函數(shù):

      1.bb=xlsread('c:feature.xls','a0:an40'),其中:c:feature.xls為文件存放的地址,a0:a40為將要讀取的單元格的范圍.bb為讀取的矩陣在MATLAB中的變量名.

      2.使用m文件腳本如下: 

         Excel =actxserver('Excel.Application');

         set(Excel,'Visible', 1);

         Workbooks =Excel.Workbooks;

         Workbook =invoke(Workbooks, 'Open',[cd,'\feature\ABC.xls']);

%%  讀取 ABC.xls:sheet1  a1( R1C1)~an40( R240c40) 范圍內的 40by40 矩陣

        read_excel=ddeinit('excel','ABC.xls:sheet1');

        feature1 =ddereq(read_excel, 'R1c1:R40c40');

        feature1

%%  關閉ABC.xls

        invoke(Excel,'Quit');

        delete(Excel);

 

        注意:在使用時將m文件與xls文件存于同一個目錄下.另外:sheet1:可以重命名,且讀取sheet的名稱要和實際存放的名稱相同.

 

matlab讀取excel,txt文件函數(shù)

注意matlab不識別中文,讀寫的文件中最好不含有中文

excel讀取函數(shù) xlsread

text  讀取函數(shù)csvread

XLSREAD Get data and text from a spreadsheet in an Excelworkbook.
   
 [NUMERIC,TXT,RAW]=XLSREAD(FILE)reads the data specified in the Excel 
   
 file,FILE. The numeric cells in FILE are returned in NUMERIC, thetext 
   
 cellsin FILE are returned in TXT, while the raw, unprocessedcell 
    contentis returned in RAW. 
 
 

    [NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE)reads the data specified
   
 inRANGE from the worksheet SHEET, in the Excel file specified inFILE. 
   
 It ispossible to select the range of data interactively (seeExamples
   
 below).Please note that the full functionality of XLSREAD dependson 
   
 theability to start Excel as a COM server fromMATLAB. 
 

    [NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE,'basic')reads an XLS file as
   
 above,using basic input mode. This is the mode used on UNIXplatforms
   
 as well ason Windows whenExcel is not available as a COM server.  
   
 In thismode, XLSREAD does not use Excel as a COM server, whichlimits
   
 importability. Without Excel as a COM server, RANGE will beignored
   
 and,consequently, the whole active range of a sheet will beimported. 
   
 Also,in basic mode, SHEET is case-sensitive and must be astring.
 

    [NUMERIC,TXT,RAW]=XLSREAD(FILE,SHEET,RANGE,'',CUSTOMFUN)
   
 [NUMERIC,TXT,RAW,CUSTOMOUTPUT]=XLSREAD(FILE,SHEET,RANGE,'',CUSTOMFUN)
   
 Whenthe Excel COM server is used, allows passing in a handle toa
    customfunction. 
 Thisfunction will be called just beforeretrieving 
   
 theactual data from Excel. It must take an Excel Range object (e.g.of
   
 type'Interface.Microsoft_Excel_5.0_Object_Library.Range') asinput,
    andreturn one as output. 
 Optionally,this custom function may return
   
 asecond output argument, which will be returned from XLSREAD asthe
    fourthoutput argument, CUSTOMOUTPUT. 
 Fordetails of what is possible 
   
 usingthe EXCEL COM interface, please refer to Microsoftdocumentation.
 

    INPUTPARAMETERS:
   
 FILE:string defining the file to read from. Default directory ispwd.
         
 Defaultextension is 'xls'.
   
 SHEET:string defining worksheet name in workbook FILE.
          
 doublescalar defining worksheet index in workbook FILE. See
          
 NOTE1.
   
 RANGE:string defining the data range in a worksheet. See NOTE2.
    MODE:string enforcing basic import mode. Valid value ='basic'. 
 This
   
 is themode always used when COM is not available (e.g. onUnix).
 

    RETURNPARAMETERS:
   
 NUMERIC= n x m array of type double.
   
 TXT = rx s cell string array containing text cells in RANGE.
   
 RAW = vx w cell array containing unprocessed numeric and textdata.
   
 BothNUMERIC and TXT are subsets of RAW.
 

    EXAMPLES:
    1.Default operation: 
 
      
 NUMERIC= xlsread(FILE);
      
 [NUMERIC,TXT]=xlsread(FILE);
      
 [NUMERIC,TXT,RAW]=xlsread(FILE);
 

    2. Getdata from the default region:
      
 NUMERIC= xlsread('c:\matlab\work\myspreadsheet')
 

    3. Getdata from the used area in a sheet other than the firstsheet:
      
 NUMERIC= xlsread('c:\matlab\work\myspreadsheet','sheet2')
 

    4. Getdata from a named sheet:
      
 NUMERIC= xlsread('c:\matlab\work\myspreadsheet','NBData')
 

    5. Getdata from a specified region in a sheet other than thefirst
      
 sheet:
      
 NUMERIC=xlsread('c:\matlab\work\myspreadsheet','sheet2','a2:j5')
 
 
   
 6. Getdata from a specified region in a named sheet:
      
 NUMERIC=xlsread('c:\matlab\work\myspreadsheet','NBData','a2:j5')
 
 
   
 7. Getdata from a region in a sheet specified by index:
      
 NUMERIC= xlsread('c:\matlab\work\myspreadsheet',2,'a2:j5')
 
 
   
 8.Interactive region selection:
      
 NUMERIC= xlsread('c:\matlab\work\myspreadsheet',-1);
      
 Youhave to select the active region and the active sheet inthe
      
 EXCELwindow that will come into focus. Click OK in theData 
      
 SelectionDialog when you have finished selecting the activeregion.
 

    9.Using the custom function:
      
 [NUMERIC,TXT,RAW,CUSTOMOUTPUT]= xlsread('equity.xls', ..., @MyCustomFun)
      
 Wherethe CustomFun is defined as:
 

       function[DataRange, customOutput] = MyCustomFun(DataRange)
         
 DataRange.NumberFormat= 'Date';
         
 customOutput= 'Anything I want';
     
 
      
 Thiswill convert to dates all cells where that is possible.
 

    NOTE 1:The first worksheet of the workbook is the default sheet.If 
         
 SHEETis -1, Excel comes to the foreground to enableinteractive 
         
 selection(optional). In interactive mode, a dialogue willprompt 
         
 you toclick the OK button in that dialogue to continue inMATLAB. 
          
 (Onlysupported when Excel COM server is available.)
   
 NOTE 2:The regular form is: 'D2:F3' to select rectangular regionD2:F3 
         
 in aworksheet. RANGE is not case sensitive and uses ExcelA1 
         
 notation(see Excel Help). (Only supported when Excel COMserver 
         
 isavailable.)
   
 NOTE 3:Excel formats other than the default can also be read.
          
 (Onlysupported when Excel COM server is available.)
 

    Seealso xlswrite, csvread, csvwrite, dlmread, dlmwrite,textscan.

    Referencepage in Help browser
       docxlsread 

 CSVREADRead

 

a comma separated value file.
   
 M =CSVREAD('FILENAME') reads a comma separated value formattedfile
    FILENAME.  Theresult is returned in M. 
 Thefile can only contain
   
 numericvalues.
 

    M =CSVREAD('FILENAME',R,C) reads data from the comma separatedvalue
    formattedfile starting at row R and column C. 
 R and Care zero-
   
 basedso that R=0 and C=0 specifies the first value in thefile.
 

    M =CSVREAD('FILENAME',R,C,RNG) reads only the rangespecified
   
 by RNG= [R1 C1 R2 C2] where (R1,C1) is the upper-left corner of
    thedata to be read and (R2,C2) is the lower-rightcorner. 
 RNG
   
 canalso be specified using spreadsheet notation as in RNG ='A1..B7'.
 

    CSVREADfills empty delimited fields with zero.  Datafiles where
   
 thelines end with a comma will produce a result with an extralast 
   
 columnfilled with zeros.
 

    Seealso csvwrite, dlmread, dlmwrite, load, fileformats,textscan.

    Referencepage in Help browser
       doccsvread

 

 

 

 

Matlab如何讀取Excel 表格數(shù)據(jù)

 

Subject: 
Are there any examples that show how to use the ActiveX automationinterface to connect MATLAB to Excel? 

Problem Description 
I am trying to control Excel from MATLAB using ActiveX. Are thereany examples that show how to use the ActiveX automation interfacefrom Excel to do this? 

Solution: 
Most of the functionality that you get from ActiveX is dependent onthe object model, which the external application implements.Consequently, we are usually unable tp provide much informationabout the functions that you need to use in the remote applicationto perform a particular function. We do, however, have an examplethat shows how to do perform common functions inExcel. 

We also recommend that you become more familiar with the Excelobject model in order to better use Excel's ActiveX automationinterface from MATLAB. You can find more information on thisinterface by selecting the "Microsoft Excel Visual Basic Reference"topic in the Microsoft Excel Help Topic dialog. This topic areacontains a searchable description of Excel methods andproperties. 

The following example demonstrates how to insert MATLAB data intoExcel. It also shows how to extract some data from Excel intoMATLAB. For more information, refer to the individual comments foreach code segment. 

% Open Excel, add workbook, change activeworksheet, 
% get/put array, save, and close 

% First open an Excel Server 
Excel =actxserver('Excel.Application'); 
set(Excel, 'Visible', 1); 

% Insert a new workbook 
Workbooks = Excel.Workbooks; 
Workbook = invoke(Workbooks, 'Add'); 

% Make the second sheet active 
Sheets = Excel.ActiveWorkBook.Sheets; 
sheet2 = get(Sheets, 'Item', 2); 
invoke(sheet2, 'Activate'); 

% Get a handle to the active sheet 
Activesheet = Excel.Activesheet; 

% Put a MATLAB array into Excel 
A = [1 2; 3 4]; 
ActivesheetRange =get(Activesheet,'Range','A1:B2'); 
set(ActivesheetRange, 'Value', A); 

% Get back a range. It will be a cellarray, 
% since the cell range can 
% contain different types of data. 
Range = get(Activesheet, 'Range','A1:B2'); 
B = Range.value; 

% Convert to a double matrix. The cell array must contain onlyscalars. 
B = reshape([B{:}], size(B)); 

% Now save the workbook 
invoke(Workbook, 'SaveAs','myfile.xls'); 

% To avoid saving the workbook and being prompted to doso, 
% uncomment the following code. 
% Workbook.Saved = 1; 
% invoke(Workbook, 'Close'); 

% Quit Excel 
invoke(Excel, 'Quit'); 

% End process 
delete(Excel); 

There are several options for connecting MATLAB with Excel. For anexample that shows how to connect MATLAB with Excel using ExcelLink, please refer to the followingURL: 

http://www.mathworks.com/support/solutions/data/27338.shtml 

For an example that shows how to connect MATLAB with Excel usingDDE, please refer to the following URL: 

http://www.mathworks.com/support/solutions/data/31072.shtml 

For information on how to use the XLSREAD function to read .xlsfiles, please refer to the followingURL: 

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xlsread.shtml

 

MatlabGUI中讀取數(shù)據(jù)或其它文件

假設在GUIinMatlab)中設計好一按鈕,點擊以后彈出對話框,并希望獲取來自電腦上任一文件夾下的數(shù)據(jù)或其它文件。

做法:假設要讀取在桌面上一名叫ATR_ALL.dat的數(shù)據(jù)文件,并將其值賦到Data變量,其程序代碼為:

[filename, pathname] =uigetfile;
if (filename==0 &pathname==0)
    msgbox('您沒有選擇文件,請重新選擇!','打開文件出錯','error');
else
    NumericalDatum=load([pathname,filename]);    

%NumericalDatum=xlsread([pathname,filename]);

然后把從計算機其它文件夾讀取的數(shù)據(jù)文件保存到當前文件夾
    saveNumericalDatum.dat NumericalDatum-ascii;

%xlswrite('NumericalDatum.xls',NumericalDatum);

    msgbox('打開及讀取數(shù)據(jù)完畢!','確認','warn');
end

......

在新的應用文件中

最后再從當前文件夾將數(shù)據(jù)讀取出來,用于當前程序
loadNumericalDatum.dat;

%NumericalDatum=xlsread('NumericalDatum.xls');

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
matlab 讀取 excel
matlab打開excel (xlsread、xlswrite)實用方法...
xlsread讀取xls文件數(shù)據(jù)
MATLAB讀寫Excel
Write to Excel Sheet through Matlab
利用jxl.jar包操作excel
更多類似文章 >>
生活服務
熱點新聞
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服