Comparing strings in two different tables with eachother

조회 수: 6 (최근 30일)
Martin Mansour
Martin Mansour 2017년 4월 26일
편집: Peter Perkins 2017년 4월 26일
I'm helping a friend out with a script, but I can't say that I know it all due to the data, which she has, is in a table. She is getting two different tables, and the first column will contain different strings showing day/month/year hour/minute/second. She wants a script that can check if some of the time data are the exact same in the two tables, and if they are, the row in the second table will be imported into some extra columns added in the end of the first table.
I personally have it all in my head on how it should be working, but the fact that the data is in a table, and probably also because it's strings, makes it hard for me to get this to work as I wanted. Right now I'm just testing if the script can find a certain date and time with the following code, but it doesn't seem to find it.
H = height(data_table);
for i = 1:H;
if strcmp('07/04/17 02:55:00',data_table(i,1)) == 1
fprintf('Found! \n')
else
fprintf('Nothing! \n')
end
end
It should find the date and time, but it doesn't seem to print anything else than 'Nothing!'

채택된 답변

Stephen23
Stephen23 2017년 4월 26일
편집: Stephen23 2017년 4월 26일
if strcmp('07/04/17 02:55:00',data_table{i,1})
^ ^
  댓글 수: 3
Stephen23
Stephen23 2017년 4월 26일
편집: Stephen23 2017년 4월 26일
A table is a container for data, just like a cell array or a structure, but it is not the data itself. The difference is important when working with all container types.
When you use () on an array you obtain a subset of that array (i.e. a cell if the array is cell, a table if the array is table, numeric if the array is numeric, etc). Sometimes it is very useful to access part of a container, without caring what is inside the container.
When you use {} you are getting the data (another array) out of the container array (i.e. some data out of a cell array, come data out of a table, etc).
Peter Perkins
Peter Perkins 2017년 4월 26일
편집: Peter Perkins 2017년 4월 26일
Perhaps even more readbale:
if strcmp('07/04/17 02:55:00',data_table.YourVarName(i))
But actually, you should consider using a datetime variable instead of text, which would allow
if data_table.YourVarName(i)) == '07/04/17 02:55:00'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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