Comparing strings in two different tables with eachother

7 次查看(过去 30 天)
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