MATLAB表格基本操作


主要總結了一下常用的表格操作:
創建一個表格table.csv,其內容是:

%讀取csv文件
T1=readtable('table.csv');
T1
%讀取不含列標題的csv文件
T2=readtable('table.csv','ReadVariableNames',false);
T2
%如果數據是被空格進行分隔的
%T = readtable(filename,'Delimiter',' ','ReadVariableNames',false)
% 讀取表行的名字

T3=readtable('table.csv','ReadVariableNames',false,'ReadRowNames',true);
% 表的基本操作(對於第一列其實是cell數組的操作)
T1.Var1

%注意區別以下兩個的區別
T1.Var1(1)
T1.Var1{1}

%% 表的基本讀取操作
%讀取特定列
T1.Alice
%讀取特定行(使用strcmp來選擇讀取哪一行)
disp('________________________________important________________________________');

find(strcmp(T1.Var1, 'two'))
%比如說讀取two那一行Alice的數據,是4
T1.Alice(find(strcmp(T1.Var1,'two')))
%find(T1.Var1=='one')
%T1.Alice(find(T1.Var1=='one'))
disp('________________________________end________________________________')
%% 檢測針對電子表格文件的導入選項,指定要導入的變量,然后讀取數據。
% 根據文件創建導入選項對象。
opts = detectImportOptions('table.csv')
opts.SelectedVariableNames = {'Alice'};
T4=readtable('table.csv',opts);%這里就只讀取了Alice的數據
T4

%% 表格的創建與保存
index=T1.Var1;
Alicemy=T1.Alice;
Bobmy=T1.Bob;

TT=table(index,Alicemy,Bobmy);
writetable(TT,'mytable.csv');

運行結果如下:

T1 =

  2×4 table

     Var1      Alice    Bob    Cindy
    _______    _____    ___    _____

    {'one'}      1       2       3  
    {'two'}      4       5       6  


T2 =

  2×4 table

     Var1      Var2    Var3    Var4
    _______    ____    ____    ____

    {'one'}     1       2       3  
    {'two'}     4       5       6  


ans =

  2×1 cell 數組

    {'one'}
    {'two'}


ans =

  1×1 cell 數組

    {'one'}


ans =

    'one'


ans =

     1
     4

________________________________important________________________________

ans =

     2


ans =

     4

________________________________end________________________________

opts = 

  DelimitedTextImportOptions - 屬性:

   格式 屬性:
                    Delimiter: {','}
                   Whitespace: '\b\t '
                   LineEnding: {'\n'  '\r'  '\r\n'}
                 CommentStyle: {}
    ConsecutiveDelimitersRule: 'split'
        LeadingDelimitersRule: 'keep'
                EmptyLineRule: 'skip'
                     Encoding: 'UTF-8'

   替換 屬性:
                  MissingRule: 'fill'
              ImportErrorRule: 'fill'
             ExtraColumnsRule: 'addvars'

   變量導入 屬性:
                VariableNames: {'Var1', 'Alice', 'Bob' ... and 1 more}
                VariableTypes: {'char', 'double', 'double' ... and 1 more}
        SelectedVariableNames: {'Var1', 'Alice', 'Bob' ... and 1 more}
              VariableOptions: Show all 4 VariableOptions 
	Access VariableOptions sub-properties using setvaropts/getvaropts
        PreserveVariableNames: false

   位置 屬性:
                    DataLines: [2 Inf]
            VariableNamesLine: 1
               RowNamesColumn: 0
            VariableUnitsLine: 0
     VariableDescriptionsLine: 0 
	要顯示該表的預覽,請使用 preview


T4 =

  2×1 table

    Alice
    _____

      1  
      4  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM