Index exceeds Matrix Dimension

I'm following a comparatively larger code and I'm stuck in this section.
T1=num2str(TTime);
if length(T1)==9
T1=['0',T1];
end
T11=datenum([str2double(T1(1,1:4)) str2double(T1(1,5:6)) str2double(T1(1,7:8)) str2double(T1(1,9:10)) str2double(T1(1,11:12)) str2double(T1(1,13:14))]);
Here, TTime is an array of numbers that specifies a date/time. (Ex: 20140705001529. i.e. year,month,date,hour,minutes,second) But I keep getting the error "Index exceeds matrix dimensions." Initially, str2double was replaced with str2num.
I know this snippet might not be enough to figure things out but any help from you guys is appreciated! If you have any suggestions, please do let me know.
Thanks in advance.

6 comentarios

Adam
Adam el 11 de Dic. de 2019
Obvious question is are you sure T1 always has at least 14 elements?
Jake
Jake el 11 de Dic. de 2019
Yes! I checked more than twice, hence the struggle :(
Stephen23
Stephen23 el 11 de Dic. de 2019
Editada: Stephen23 el 11 de Dic. de 2019
"...are you sure T1 always has at least 14 elements?"
Judging by the code, sometimes it is expected to have 10 elements:
if length(T1)==9
T1=['0',T1];
end
"TTime is an array of numbers..."
Calling num2str on an array of numbers creates a character array whose size depends on the values in that numeric array, with multiple spaces separating the columns. It is not robust code to assume anything about its size.
Without an accurate description of TTime it is hard to advise what alternatives you could try. Best would be if you click the paperclip button to upload TTime in a .mat file.
Jake
Jake el 11 de Dic. de 2019
Do you have another suggestion instead of "num2str"? :(
Adam
Adam el 11 de Dic. de 2019
You should be able to find out why you are getting the error trivially using the Pause on Errors option from the Run menu. It will stop the code at the line of the error. Then you can simply look at the relevant components in the workspace or on command line. How to fix it is another matter, but simply to know what the cause is should be very simple.
Jake
Jake el 12 de Dic. de 2019
Yes. I get the line where the error happens and the error says "Index exceeds matrix dimensions."
I couldn't figure out the reason yet. I'm still a newbie so pardon me if I'm making naive comments :(

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 11 de Dic. de 2019
Editada: Stephen23 el 11 de Dic. de 2019
Simpler, more robust code:
>> N = 20140705001529; % Ugh... dates should not be stored like this!
>> S = sprintf('%d',N);
>> S(end+1:14) = '0'; % ensure 14 digits
>> T = datenum(S,'yyyymmddHHMMSS')
T =
7.357850107523148e+05
Checking:
>> datevec(T)
ans =
2014 7 5 0 15 29
Or even better using datetime:
>> T = datetime(S,'InputFormat','yyyyMMddHHmmss')
T =
05-Jul-2014 00:15:29

5 comentarios

Jake
Jake el 11 de Dic. de 2019
Editada: Jake el 11 de Dic. de 2019
I know! :( However, I'm using data which are already saved as .txt files. Within the file, the dates are stored like that.
I will try this. However, it'd be ideal if there is another way to figure out why am I getting the error.
Thank you very much.
Edit:
I tried using datetime instead of datenum (I'm not sure that's what you mean. Kind of a newbie, apologies.) But I got this error. Now I'm more confused.
Year, month, day, hour, and minute components must be integer values.
Stephen23
Stephen23 el 11 de Dic. de 2019
"Within the file, the dates are stored like that."
Sure... then why not just import the dates as char or datetime directly? Having that intermediate numeric value is unlikely to be required.
Jake
Jake el 12 de Dic. de 2019
.txt file has other data as well. The code retrieves each component at some point and the problem comes with the "dates" component.
Capture.JPG
Thank you for all the suggestions. I know it's not easy to troubleshoot with a piece of code but you have helped so far :)
Stephen23
Stephen23 el 12 de Dic. de 2019
Editada: Stephen23 el 12 de Dic. de 2019
".txt file has other data as well."
I don't see why that matters: textscan, readtable, etc. let you specify the formats for imported data, including importing dates as text or datetime. You should fix your file importing.
Jake
Jake el 12 de Dic. de 2019
Hm. I will try the suggestions. Thank you so much for the help.
By the way, Can you please explain to me what this line does, exactly? It is taken from the part of the code that I have added earlier (OP). I have an idea, of course, but I want your opinion. Again, apologies for the rookie questions.
T11=datenum([str2double(T1(1,1:4)) str2double(T1(1,5:6)) str2double(T1(1,7:8)) str2double(T1(1,9:10)) str2double(T1(1,11:12)) str2double(T1(1,13:14))]);

Iniciar sesión para comentar.

Categorías

Preguntada:

el 11 de Dic. de 2019

Comentada:

el 12 de Dic. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by