MATLAB Answers

0

Trouble reading the file with strread and textscan

Asked by James James on 15 Oct 2019
Latest activity Commented on by James James on 17 Oct 2019
So I'm really new to MATLAB and (un)fortunately, I have to troubleshoot a pretty complicated program/code while learning. Obviously I'm encountering a lot of errors and this one I cannot overcome. Following is the part of the code and I hope the contec\xt will be enough.
DataDate=dir('C:**file path in included here**/');
DayCount=0;
NextMMSI='';
AveCount=0;
AveCount10=0;
AveCount3=0;
for viv=1:length(DataDate)-3
DayCount=DayCount+1;
length(DataDate)-3-viv
kkk=DataDate(viv+3).name;
fileID = fopen(['C:/**file path in included here**/',DataDate(viv+3).name]);
Year = strread(kkk(1:4));
MONTH = strread(kkk(5:6));
DATE1=strsplit(DataDate(viv+3).name,'.');
DATE2=cell2mat(DATE1(1,1));
Code goes on but the program stops with the error;
Error using dataread
Trouble reading number from file (row 1, field 1) ==> ._.D
Error in strread (line 48)
[varargout{1:nlhs}]=dataread('string',varargin{:}); %#ok
I have tried changing "strread" to "textscan" as the program suggested but there's no luck.
Hope you might have some advice. TIA!

  11 Comments

MATLAB R2017a, if that's what you were asking :)
This error is related to another part or the code, I persume. There's another series of if/end commands here. I'll post it here if I don't solve it.
Thanks again for all the support, I really appreciate the quick responses.
Yes, R2017a is a perfect answer. Thus, the problem isn't related to end as an index.
Oh okay. Which means the problem is from the code itself. Thanks for the clarification. I'll try troubleshooting it.

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 15 Oct 2019

DataDate=dir('C:**file path in included here**/');
for viv=1:length(DataDate)-3
That code is incorrect. It is an attempt to skip the . and .. entries, but the . and .. entries are not certain to be the first two entries. Your code is also not skipping directories, which is your most immediate problem: you have a directory that you are attempting to read from as if it is a file, and the directory happens to contain a file or directory named ._.D
DataDate=dir('C:**file path in included here**/');
DataDate([DataDate.isdir]) = []; %remove all directories including . and ..
and then loop without skipping any entries.

  2 Comments

Thank you very much! I will try this one as well!
Thanks again.
Edit: I wasn't very clear about "attempt to skip the . and .. entries, but the . and .. entries are not certain to be the first two entries".
(again, my apologies, I'm very new and trying something quite complex, as far as I understand)
I tried the changes which you've suggested but I'm still getting the same error without any changes :(. I'll keep trying and will post here if I get any updates.
Thank you!
When you dir() a directory name, at some point in the results, there will be an entry named '.' and another entry named '..' . Much of the time those are the first two entries. It is common for people to skip those two entries by starting using from the third directory entry like your code does. However that is a bug: none of the operating systems and file systems that MATLAB is supported on promise that '.' and '..' will be the first two entries, and several of the combinations MATLAB is supported on promise that those will not be the first two entries under some reasonable circumstances. You are using MS Windows, which makes no promises about the order entries are returned in, but most of the time '.' and '..' might not be the first two entries if there are files or directories named starting with any of the character space or !"#$%&'()*+,-. There is evidence that your directory has entries named starting with '.' and your code does not expect that possibility and is ending up reading a directory as if it were a file.

Sign in to comment.


Answer by per isakson
on 16 Oct 2019
Edited by per isakson
on 17 Oct 2019

"Illegal use of reserved keyword "end". "
Deleting some code may leave a spurious end sometimes far to the right outside the visible pane.
This code snippet throws the error
Error: File: Limitations.m Line: 6 Column: 29
Illegal use of reserved keyword "end".
The Code Analyzer Report might be helpful
The Code Analyzer Report processes one full folder at a time. The result may look overwhelming, but why not give it a try. The numbers to the left of the messages are hyperlinked to the lines in the m-file.
Afterthought
You wrote: "Which means the problem is from the code itself". Yes, most likely it's a syntax error that the Code Analyzer detects before the code is executed.
The error message always(?) comes together with a line telling which line of code throw the error. e.g.
Error: File: Limitations.m Line: 6 Column: 29
Illegal use of reserved keyword "end".
First thing is to search that line for a syntax error.

  9 Comments

"Illegal use of reserved keyword "end". " and "the error message didn't come with a line that tells where the error is"
Is it this error message that came without a name and line number? That happens for errors in code invoked by timer-objects.
"I just double-checked and the Smart Ident Feature is applied. Still end up getting the aforementioned error."
I'm lost, I need to look over your shoulder or run your code myself. What happens if you run
  1. clearvars
  2. dbstop if caught error
  3. dbstop if error
  4. the code that throws the error, "Illegal use of reserved keyword "end".
???
The error did come with a line number and it is similar to what you have mentioned.
(I know I'm not being very clear by not producing the .m file but truth be told, it is a part of a project :( So I'm reluctant to submit the file or I'll be in trouble. Apologies. But you guys have been a real help so far. I've overcome alot of errors thanks to this thread it self!)
I'll keep working with the suggestions. Big thanks to both of you. Means a lot!

Sign in to comment.