Code Not working...sort of
显示 更早的评论
I have a very long script that works, but not all the way through. When I run the non working part on its own, it works just fine. I have no idea what is going on and why it doesn't work when I press run, but works when I copy and paste the script into the command window. I don't get any errors. I just don't get the plot that I want, which is a change of atmospheric pressure over time. I tried setting the xlimits, but it doesn't let me in a scatter plot. It would also be a high inconvenience as I need to run the script for multiple months over multiple years.
I have attached my script as it is rather long and an example of the type of files I am working with. There are several hundred thousand that will need to be processed. In the script they are not text files, but I needed to make it one to attach it here.
I have also attached two image files, one showing the incorrect graph in the last subplot and the other showing the correct graph.
If anyone can help me to figure out what is going on, I would appreciate it. I am still new to matlab and I am not completely versed in why the graph is created incorrectly when I press Run, but not when I copy and paste it into the command window.
24 个评论
C G
2018-8-6
dpb
2018-8-6
The problem with the third plot is the x-axis range...all the data are there the xlim values are such it's all visually compressed as the range is 1800-2000 instead of 1990.x
Use debugger and see where you didn't set xlim correctly.
As far as the issue between the two conditions; again use the debugger from the script and work your way through to see what goes wrong where.
"I tried setting the xlimits, but it doesn't let me in a scatter plot."
That's simply not true; there's nothing unique about scatter plot axes but you don't show where you made any call to xlim to see what the issue might be in your syntax.
Looking at the data file it appears the first portion is a date vector but there are only nine times given but there are many more points than that on the plots -- where's the rest of the data come from or did you just truncate a file to illustrate format?
Image Analyst
2018-8-6
I can't even run your code because you didn't give code to define pathdist, decyear, kmmetersTraveled1, and maybe some other variables. Please include all code needed to run it.
It made me chuckle when you said your 72 line long code was "very long". You should see my programs. A typical program is several thousand lines of code.
dpb
2018-8-6
Use the debugger...stepping through will likely uncover the cause of the difficulties.
Adam Danz
2018-8-6
Learning how to debug in matlab will solve these types of problems. Here's a tutorial and there are several videos out there you can search for, too.
When in debug mode, it's helpful to use these shortcuts:
- F9: run the section that is highlighted
- F10: run the next line of code
- F11: run the next line of code and step into the function it calls
- F5: Run the code until the next break point or until the end; also, start running the code.
- F12: toggle (on/off) a break point at current line of code.
C G
2018-8-6
dpb
2018-8-7
for i = 3625:4344 %Set this to the number of files you want to import.
C = importdata(tdumpnamesOneHr(i),delimiterIn,headerlinesIn);
z1Data = [z1Data C.data(:,3:12)];
end
This looks pretty risky to me...first of all having to fixup the loop limits and then where's the tdumpnamesOneHr array come from?
Using dynamic reallocation to "grow" the array is exceedingly overhead-intensive; at least allocate it to the size based on the loop limits.
Also, the sample data file has a count record at the beginning but the 'headerlines' is fixed and ignores that inside the file; is it absolutely inviolate that there are always 9 of those initial records?
Even if you're going to do this, you've made things much more difficult indexing-wise by concatenating horizontally on one long record instead of vertically to make each
Also, what's the 3:12 subset; what variable is what column and what do the two records represent?
My initial best guess would be that somewhere in here is where things go wrong in that all the assumptions made don't hold up in court for every case.
dpb
2018-8-7
You're not really answering the fundamental question -- what's the file naming convention--you should be able to get the desired files for whatever dates wanted simply by giving that date range and then retrieving the correct files by building the file name range.
I'm fully aware the header lines will be there; the issue is as you say "They can have less, but that will change the output of the data" which is exactly the issue I was asking about; you need to read that initial record to find out how many header lines are actually in the file.
Again, I understand what 3:12 indexing is; the question was what are the data in the file because the header doesn't seem to make any sense as compared to what the values are on a 1:1 basis. I guessed the date vector off first six; the question was what are the others and then what is the actual end result desired to do with them. Altho it's peculiar if the date vector is actually yr,mo,da,hr,mn,se that they're in reverse order in the sample file as the first would be 10:00:00 and the second 9:00:00 unless there's an issue that they wrote the file as AM/PM and didn't encode that minor piece of data as to which it actually is and so that is 10AM and 9PM. That's a real piece o' work if that's the case...some like this you could infer if T2<T1 but others might be totally indeterminate, depending.
Since that is the way the data are organized as I suspected, concatenating horizontally instead of vertically makes no sense at all.
As far as the last comment, I'm positive there's a much easier and simpler way to read the files reliably; it revolves around how they are named and if that naming was reasonable such that they can be identified from the date wanted; there's no issue in determining how many days are in any month of any year as far as the number; there's no reason to have to manually hardcode that in and change the function itself.
What is needed is the actual base problem definition from which to develop the problem specification and then the solution can be coded.
" why doesn't it work when I press run and not when I run just the problem section on its own."
Don't be so defensive on the file structure; I'm not trying to change anything but have to understand it well enough to be able to write code to parse it reliably and part of that is knowing just what it is in the file/data that needs be treated, and how.
Undoubtedly related to the data and the code and the context but can't solve one without knowing how to do the other correctly. When that's done, it'll all work all the time automagically provided all the variables have been correctly accounted for.
You also have to recognize that we only know what we're told here, we haven't worked on the problem a priori so don't know all the details and can't tell what is and what is (or may) not be important. Only ascertaining such leads to nirvana in the end...for instance, how were we to have any idea about reversed time w/o asking Q???
dpb
2018-8-7
Glad to hear..."real world" interfered before I got back to try to actually write code, sorry.
I can't actually run the full code even if had; the TB requires the Mapping TB I don't have.
I'm still pretty sure there was/is a clear reason, just didn't have the time to get far enough along to actually uncover it...if it pops up again, repost and somebody if not I is bound to come along again.
dpb
2018-8-7
Understand on the intent...just weren't answering the questions trying to get to the bottom of the "how and why"... :)
One thing from the attaching the file-naming description and example of the files list; it's trivial to retrieve the files for any year or year/month or year/month/day simply by giving the date information
yr=90;
mo=2;
fnWild=num2str([yr mo],'tdump%02d%02d*%10'); % filename wildcard string
d=dir(fnWild); % retrieve those matching
for i=1:length(d) % iterate over the list
data=importdata(d(i).name, ... % retrieve each and process in turn
I've got meetings in town again at the moment but that logic would help at least some in the repetitive fixups. I'll have to think a little more about how to build a range; of course to read everything you have on hand is trivial, just use
d=dir('tdump*');
C G
2018-8-8
dpb
2018-8-8
That was just showing a structure by which you can read the data without the counting by using the file naming convention to select files...as noted, meetings interrupted precluding more details at the time...
dpb
2018-8-9
Well, would need to see the actual code and error in context to decipher the above; that's the lower-level error in the ML routine, not the top-level user code.
I had meetings out of town that took all of today; mayhaps tomorrow I can try to flesh that out a little...basically it carries on from there with your code as starters, all I did was to replace having to manually count and build file names manually by using a wild card string to return the directory structure contaning the list...I ran out of time at that point...
C G
2018-8-9
Ah, a typo...I shoulda' pasted instead of re-typing. The image of the file names list has the trailing "%10" string; is that real? The above assumed it was and might be significant in selecting a specific subset of files. But, to incorporate a "%" in a format string as a literal it has to be "doubled up" and I missed doing it there...
>> yr=90;mo=2; % random yr, month for demo
>> fnWild = num2str([yr mo],'tdump%02d%02d*%%10');
>> fnWild
fnWild =
'tdump9002*%10'
>>
If that isn't a significant piece or isn't really part of the file names (your attached sample file didn't have such) then you can just stop with the wildcard "*"
fnWild = num2str([yr mo],'tdump%02d%02d*');
It also assumes there's not a file extension but perhaps from the example attached there should be a '.txt' added on the end?
fnWild = num2str([yr mo],'tdump%02d%02d*.txt');
C G
2018-8-9
dpb
2018-8-10
A. OK that 'splains the file naming. The % while unorthodox may not be a bad thing to have in there at that point to make visualization a little easier--having two more digits tacked onto the base name would really make it hard to scan visually. You could, of course, just use REN to do a substitution to make it go away...
B. I'm working on it... :) I have yet another meeting this evening here in a few minutes and had to spend the day catching up on farm work after rain and being gone yesterday so "all's been quiet on this front" for most part...
I did download the toolset from FEX so at least should be able to run the functions for the file or two that do have when get that far along...
C G
2018-8-10
回答(1 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!