Trouble reading the file with strread and textscan

1 Ansicht (letzte 30 Tage)
Jake
Jake am 15 Okt. 2019
Kommentiert: Jake am 17 Okt. 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!
  10 Kommentare
per isakson
per isakson am 16 Okt. 2019
Yes, R2017a is a perfect answer. Thus, the problem isn't related to end as an index.
Jake
Jake am 16 Okt. 2019
Oh okay. Which means the problem is from the code itself. Thanks for the clarification. I'll try troubleshooting it.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 15 Okt. 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 Kommentare
Jake
Jake am 15 Okt. 2019
Bearbeitet: Jake am 15 Okt. 2019
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!
Walter Roberson
Walter Roberson am 16 Okt. 2019
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.

Melden Sie sich an, um zu kommentieren.


per isakson
per isakson am 16 Okt. 2019
Bearbeitet: per isakson am 17 Okt. 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 Kommentare
per isakson
per isakson am 17 Okt. 2019
Bearbeitet: per isakson am 17 Okt. 2019
"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".
???
Jake
Jake am 17 Okt. 2019
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by