Tubbles
Tubbles

Reputation: 59

Matlab Substring as function call?

I am trying to figure out this substring feature and whether there exists a function call for it or not. As far as I can find, here and here along with various other places, there simply is no related function for this since strings are char arrays, and so they settled for only implementing the indexing feature.

MWE:

fileID = fopen('tryMe.env');
outDate = fgetl(fileID);

Where the file 'tryMe.env' only consists of 1 line like so:

2014/9/4

My wanted result is:

outDate = 
    '14/9/4'

I am trying to find a clean, smooth one liner to go with the variable definition of outDate, something along the lines of outDate = fgetl(fileID)(3:end);, and not several lines of code.

Thank you for your time!

Upvotes: 0

Views: 112

Answers (1)

JonB
JonB

Reputation: 358

For the specific example you gave, seems like

outDate=textscan(fileID, '%*2c%infc')

would do what you want ( skip 2 characters, then read until end of line).

If you are trying to read a date, which you will later want to manipulate as a date, like comparing, differencing, etc, you could also use datenum on your fgetl line. Or, if you want a normalized date string, For instance

outDate=datestr(datenum(fgetl(fileID)),'yy/mm/dd')

would produce the string '14/09/04'

Upvotes: 1

Related Questions