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.

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)

scatter(DateNumber2(2,:),z1Data(2,j+12),sz,'b.');

end

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 24 Comments

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_596782

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_596975

## jonas (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_596979

## Image Analyst (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_596995

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597021

## Adam Danz (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597037

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597154

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597159

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597178

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597187

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597193

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597198

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597214

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597403

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597504

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597541

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597701

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597919

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597940

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_597946

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_598065

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_598302

## dpb (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_598305

## C G (view profile)

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/413668-code-not-working-sort-of#comment_598308

Sign in to comment.