How do I separate a string with hyphen and colon separation within cell

4 views (last 30 days)
When I am trying to read the file enclosed using the code below, the 1st and 2nd entries I am getting is an array of cells
with yyyy-mm-dd
and second with HH:MM:SS
I wish if there were any novel ways to get an array of cells separately with yyyy mm dd (without hyphen) and HH MM and SS (without :)
stated simply I wish I could have a work around to remove hyphen and colon and get each srting separate withing the cells.
Sorry if I sound gibberish!
code:
close all;
clear all;
fileID=fopen('RD-180531-160600.txt');
C=textscan(fileID,'%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%7.4f%7.4f%7.4f%[^\n]','HeaderLines',1);
fclose(fileID)
whos C

Accepted Answer

Rik
Rik on 15 Apr 2019
If you know the pattern you can simply use that to edit the FormatSpec:
fileID=fopen('RD-180531-160600.txt');
FormatSpec='%4s-%2s-%2s %2s:%2s:%2s %f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%7.4f%7.4f%7.4f%[^\n]';
C=textscan(fileID,FormatSpec,'HeaderLines',1);
fclose(fileID);
whos C

More Answers (0)

Categories

Find more on Characters and Strings 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!