MATLAB Answers

C G
0

Code Not working...sort of

Asked by C G
on 6 Aug 2018
Latest activity Edited by C G
on 10 Aug 2018
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 Comments

C G
on 9 Aug 2018
The % sign is both a typo and a way of designating the files by group. In my original windows command line script, there is a variable defined %HT%. When the variable is created by the computer it skips the first % but when it generated the name of the file, it kept it in. It was easier to leave it then figure out the problem.
The files are indeed without extension. What comes after this?
for i=1:length(d) % iterate over the list
data=importdata(d(i).name, ... % retrieve each and process in turn
dpb
on 10 Aug 2018
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
on 10 Aug 2018
No worries on time. I am slowly working through my code and getting stuff that looks fine.
The naming convention has been a blessing. I have 5 %HT that I am working with. It makes my life easier if I accidentally mix up the variables, I can easily sort through them. Yay! Happy accident!

Sign in to comment.

1 Answer

Answer by C G
on 7 Aug 2018
Edited by C G
on 10 Aug 2018

delimiterIn = ' ';
headerlinesIn = 13;
% Create an empty Table that will hold ALL imported data
z1Data = [];
% Use 'for loop' to import all the data into one place
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:15)];
end
%Now let's loop through all of the kmmetersTraveled.
kmmetersTraveled1 = pathdist(z1Data(:,8),z1Data(:,9),'km');
DateNumber = 1900 + (decyear([z1Data(:,1),z1Data(:,2),z1Data(:,3),z1Data(:,4),z1Data(:,5),z1Data(:,6)]));
figure
subplot(2,2,1)
sz = 15;
scatter(DateNumber(2,:),kmmetersTraveled1(2,:),sz,'b.','filled');
box off;
xlabel('time')
ylabel('kilometers traveled')
hold on;
for j = 14:13:size(z1Data,2)
DateNumber = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
kmmetersTraveled = pathdist(z1Data(:,j+7),z1Data(:,j+8),'km');
scatter(DateNumber(2,:),kmmetersTraveled(2,:),sz,'b.');
end
%Now calculate and plot speed in m/s.
subplot(2,2,2)
speed_1 = (((kmmetersTraveled)*1000)/3600);
DateNumber = 1900 + (decyear([z1Data(:,1),z1Data(:,2),z1Data(:,3),z1Data(:,4),z1Data(:,5),z1Data(:,6)]));
scatter(DateNumber(2,:),speed_1(2,:),sz,'b.','filled');
box off;
xlabel('time')
ylabel('speed (m/s)')
hold on;
for j = 14:13:size(z1Data,2)
DateNumber = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
kmmetersTraveled = pathdist(z1Data(:,j+7),z1Data(:,j+8),'km');
speed_1 = (((kmmetersTraveled)*1000)/3600);
scatter(DateNumber(2,:),speed_1(2,:),sz,'b.');
end
%Plot Atmospheric Pressure
subplot(2,2,3)
DateNumber2 = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
%scatter(DateNumber2(1,:),z1Data(1,11),sz,'r.'); hold on;
scatter(DateNumber2(2,:),z1Data(2,11),sz,'b.');
box off;
xlabel('time')
ylabel('hPa')
hold on;
for j = 14:13:size(z1Data,2)
DateNumber2 = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
%scatter(DateNumber2(1,:),z1Data(1,j+10),sz,'r.'); hold on;
scatter(DateNumber2(2,:),z1Data(2,j+10),sz,'b.');
end
%Plot Temperature.
subplot(2,2,4)
DateNumber2 = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
scatter(DateNumber2(2,:),z1Data(2,13),sz,'b.');
box off;
xlabel('time')
ylabel('Degrees (°C)')
hold on;
for j = 14:13:size(z1Data,2)
DateNumber2 = 1900 + (decyear([z1Data(:,j),z1Data(:,j+1),z1Data(:,j+2),z1Data(:,j+3),z1Data(:,j+4),z1Data(:,j+5)]));
scatter(DateNumber2(2,:),z1Data(2,j+12),sz,'b.');
end

  0 Comments

Sign in to comment.