Import files into Matlab and compare values
Show older comments
So I have a spreadsheet with lots of data on it, and I want to compare the dates and times (of experiments) on this document to the dates and times of files produced by the experiments. I use:
dir('filepath');
to obtain the struct with the info of the files and I have imported the spreadsheet. The issues arise here:
- The dates and times of the spreadsheet are in separate columns, and I can't import the times as times (they are imported as numbers), is there a way of combining these values or am I going about it in the wrong way?
- I can't extract the entire individual field from the struct which has the datetime info, and the field is in string format. Is there a way of converting this to datetime?
- How would I compare the info?
The spreadsheet contains info from all experiments and the file contains files produced by a smaller number of recent experiments (if that helps).
Please let me know if my (theoretical) procedure is correct or not as I can't find anything online suggesting you can do the above. I can't attach any files unfortunately. Apologies if anything is unclear please let me know if I can expand more.
2 Comments
KSSV
on 7 May 2019
Attach a file so that we cann see how your data is.
Dean Kennedy
on 7 May 2019
Accepted Answer
More Answers (1)
Bob Thompson
on 7 May 2019
0 votes
The dates and times of the spreadsheet are in separate columns, and I can't import the times as times (they are imported as numbers), is there a way of combining these values or am I going about it in the wrong way?
I can't extract the entire individual field from the struct which has the datetime info, and the field is in string format. Is there a way of converting this to datetime?
You can convert strings and numbers to datetime class using datetime. If necessary, you can concatenate multiple elements together to have a single input. I have not looked at your data directly, but the following is a quick example.
spreadsheet = [01 01 2000 00 00 00]; % Midnight of January 1, 2000. Input sample, you can adjust as needed
filedates = '01/01/2000 00:00:00'; % Same time in string with different format
tmp = [spreadsheet(:,3),spreadsheet(:,1),spreadsheet(:,2),spreadsheet(:,4:6)];
% Adjusting column order of spreadsheet dates and times to match y m d h m s order.
ssdt = datetime(tmp); % Spreadsheet datetime class conversion
fdt = datetime(filedates,'InputFormat','mm/dd/yyyy hh:mm:ss'); % File information datetime class conversion
%% Note:
% Because your file date information is in a structure you will need to concatenate the information before feeding it into datetime. Instead of putting 'filedates' into the datetime input you will likely end up with something like 'vertcat(files(:).datetime'.
How would I compare the info?
The actual function of this depends on what exactly you are looking to compare, and what you want to do with the results. Generally, some for of logic indexing is what you are probably looking for. Very basic example shown below:
comparison = ssdt(ssdt == fdt);
1 Comment
Dean Kennedy
on 8 May 2019
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!