How to conditionally import data from a spreadsheet into MATLAB without using a loop
조회 수: 5 (최근 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'
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!