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

3 次查看(过去 30 天)
Given a spreadsheet with variables in columns and observations in rows, I would like to selectively import those observations into MATLAB whose value in a specified column is equal to a specified string (in this case 'K'). I am aware of the possibility to use a loop but my data set is very large and I would prefer a faster solution (logical indexing?). I am using a table variable.
I would greatly appreciate any suggestions. I have found multiple solutions online to do this for array variables, but not for table variables.
My spreadsheet looks like this:
Variable1 Variable2 Variable3
234789 234678234 'K'
98764 087632167 'V'
87641 492876437 'V'
43789234 123678923 'K'
In this example I would only want to import rows 1 and 4 (not counting headers) because they have value 'K' for Variable3.
I have tried the following:
tableName(tableName(:,3) ~= 'K', :) = []
after importing the entire dataset but I get an the following error message:
Undefined operator '~=' for input arguments of type 'table'

回答(1 个)

Mahdiyar
Mahdiyar 2015-4-5
Hi Constantin
What you can do is that first read the file and store it in a variable (as a matrix) and then edit the Matirix as you want then save it again.
Since you have both number and string in your spreadsheet, the type of variable would be structure. In this case you have to select the last column and convert it to string (character) format. Finally, you may use "strcmp" to compare the tow variables in string format, like below.
A = '12w';
B = '21e';
if strcmp(A, B)
D = 1; % A is the same as B
else
D = 0;
end
Regards

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by