簡體   English   中英

如何在不使用循環的情況下有條件地將數據從電子表格導入MATLAB

[英]How to conditionally import data from a spreadsheet into MATLAB without using a loop

給定一個在列中具有變量且在行中具有觀察值的電子表格,我想將這些觀察值有選擇地導入到MATLAB中,該值在指定列中的值等於指定的字符串(在本例中為'K' )。 我知道可以使用循環,但是我的數據集非常大,因此我希望使用更快的解決方案(邏輯索引嗎?)。 我正在使用一個表變量。

我將不勝感激任何建議。 我已經找到了多個在線解決方案來對數組變量執行此操作,但對於表變量卻不行。

我的電子表格如下所示:

Variable1    Variable2    Variable3
234789       234678234    'K'
98764        087632167    'V'
87641        492876437    'V'
43789234     123678923    'K'

在此示例中,我只想導入第1行和第4行(不計算標題),因為它們的Variable3值為'K'

導入整個數據集后tableName(tableName(:,3) ~= 'K', :) = []我嘗試了tableName(tableName(:,3) ~= 'K', :) = [] ,但Undefined operator '~=' for input arguments of type 'table'錯誤消息Undefined operator '~=' for input arguments of type 'table'我得到了Undefined operator '~=' for input arguments of type 'table'

好的,可能會有更好的答案,但這就是我所知道的。 我從來沒有能夠使用這種邏輯:

tableName(tableName(:,3) ~= 'K', :) = []

即使您在表和字符(表〜='K')之間使用了正確的邏輯比較,您可能也會在嘗試使用邏輯的方式上遇到問題。

為了不循環而做您想做的事情,我通常創建一個臨時變量,該臨時變量僅包含將確定要保留哪些行的變量列。 這是我的意思的示例...

data = {1,2,'b';3,4,'c';5,6,'d';7,8,'d'};

%find which rows where the third column is 'd'
temp = data(:,3);%create temporary variable
out = strcmp(temp,'d');%returns logical 1 where strings are the same
index = find(out);%returns the indexes of which rows contain 'd'

data = data(index,:);%now you only have the rows containing 'd' 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM