Index Error when plotting - was working now not!
Ältere Kommentare anzeigen
I am coding for a project and the figure 1 plot was working fine and then all the sudden it keep giving me:
"Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Lab1_Light_scattering_and_absorption (line 34)
plot(x_h2o, y_h2o, x_milk_front, y_milk_front, x_milk_side, y_milk_side);"
when I didn't chnage anything so I am confused.
This is the code:
h2o_light = load("Lab1_Milk_Scattering_USB2E57541_15-07-20-121.txt"); % Light Source + Water
x_h2o = h2o_light(:,1);
y_h2o_light = h2o_light(:,2);
h2o_dark = load("Lab1_Milk_Scattering_USB2E57541_15-08-22-949.txt"); % Dark Background
y_h2o_dark = h2o_dark(:,2);
% Corrected Spectra: Subtract Background
y_h2o = y_h2o_light - y_h2o_dark;
milk_front_light = load("Lab1_Milk_Scattering_USB2E57541_15-19-35-431.txt"); % Milk Front
x_milk_front = milk_front_light(:,1);
y_milk_front_light = milk_front_light(:,2);
milk_front_dark = load("Lab1_Milk_Scattering_USB2E57541_15-20-08-946.txt"); % Dark Background
y_milk_front_dark = milk_front_dark(:,2);
% Corrected Spectra: Subtract Background
y_milk_front = y_milk_front_light - y_milk_front_dark;
milk_side_light = load("Lab1_Milk_Scattering_USB2E57541_15-39-12-053.txt"); % Milk Side
x_milk_side = milk_side_light(:,1);
y_milk_side_light = milk_side_light(:,2);
milk_side_dark = load("Lab1_Milk_Scattering_USB2E57541_15-43-36-313.txt"); % Dark Background
y_milk_side_dark = milk_side_dark(:,2);
% Corrected Spectra: Subtract Background
y_milk_side = y_milk_side_light - y_milk_side_dark;
% Plot with 3 spectra overlaid
plot(x_h2o, y_h2o, x_milk_front, y_milk_front, x_milk_side, y_milk_side);
legend('Water Spectra', 'Milk Forward Spectra', 'Milk Side Spectra');
xlabel('Wavelength (nm)');
ylabel('Absorbance');
title('Light Scattering');
h2o_light2 = load("Lab1_Milk_Scattering_USB2E57541_15-54-13-352.txt"); % Light Source + Water
x_h2o2 = h2o_light2(:,1);
y_h2o2 = h2o_light2(:,2);
h2o_dark2 = load("Lab1_Milk_Scattering_USB2E57541_15-55-27-324.txt"); % Dark Background
y_h2o_dark2 = h2o_dark2(:,2);
% Corrected Spectra: Subtract Background
y_h2o2 = y_h2o2 - y_h2o_dark2;
PR_long = load("Lab1_Milk_Scattering_USB2E57541_15-59-06-383.txt"); % Phenol Red Long
x_PR_long = PR_long(:,1);
y_PR_long = PR_long(:,2);
PR_short = load("Lab1_Milk_Scattering_USB2E57541_16-03-54-358.txt"); % Phenol Red Short
x_PR_short = PR_short(:,1);
y_PR_short = PR_short(:,2);
PR1V = load("Lab1_Milk_Scattering_USB2E57541_16-21-44-291.txt"); % Phenol Red + 1 Drop Vinegar
x_PR1V = PR1V(:,1);
y_PR1V = PR1V(:,2);
PR2V = load("Lab1_Milk_Scattering_USB2E57541_16-26-13-561.txt"); % Phenol Red + 2 Drops Vinegar
x_PR2V = PR2V(:,1);
y_PR2V = PR2V(:,2);
PR3V = load("Lab1_Milk_Scattering_USB2E57541_16-29-59-054.txt"); % Phenol Red + 3 Drops Vinegar
x_PR3V = PR3V(:,1);
y_PR3V = PR3V(:,2);
PR1B = load("Lab1_Milk_Scattering_USB2E57541_16-37-15-766.txt"); % Phenol Red + 1 Drop Bleach
x_PR1B = PR1B(:,1);
y_PR1B = PR1B(:,2);
PR2B = load("Lab1_Milk_Scattering_USB2E57541_16-40-37-492.txt"); % Phenol Red + 2 Drops Bleach
x_PR2B = PR2B(:,1);
y_PR2B = PR2B(:,2);
PR3B = load("Lab1_Milk_Scattering_USB2E57541_16-43-36-069.txt"); % Phenol Red + 3 Drops Bleach
x_PR3B = PR3B(:,1);
y_PR3B = PR3B(:,2);
% Plot water spectra and Phenol Red spectra (long and short pathlengths, without vinegar/bleach) overlaid in the same figure
figure(2);
plot(x_h2o2, y_h2o2, x_PR_long, y_PR_long, x_PR_short, y_PR_short);
legend('Water Spectra', 'Long Phenol Red Specta', 'Short Phenol Red Spectra');
xlabel('Wavelength (nm)');
ylabel('Absorbance');
%plot(x_PR1V, y_PR1V, x_PR2V, y_PR2V, x_PR3V, y_PR3V, x_PR1B, y_PR1B, x_PR2B, y_PR2B, x_PR3B, y_PR3B);
% Plot absorption coefficent*pathlengths obtained for both long and short pathlengths overlaid in the same figure
P_in = y_h2o; % Light power entering the solution = spectrum measured through water
P_out_long = y_PR_long;
P_out_short = y_PR_short;
u_L_long = -log(P_out_long/P_in);
u_L_short = -log(P_out_short/P_in);
plot(x_h2o, y_h2o, x_PR_long, y_PR_long, x_PR_long, u_L_long, x_PR_short, y_PR_short, x_PR_short, u_L_short);
legend('Water Spectra', 'Long Phenol Red Specta', 'Absorbance Coefficient*Pathlength Through Long Phenol Red', 'Short Phenol Red Spectra', 'Absorbance Coefficient*Pathlength Through Short Phenol Red');
xlabel('Wavelength (nm)');
ylabel('Absorbance');
wo_RB = load("Lab1_Milk_Scattering_USB2E57541_16-51-04-898.txt"); % Without Rubber Band
x_wo_RB = wo_RB(:,1);
y_wo_RB = wo_RB(:,2);
w_RB1 = load("Lab1_Milk_Scattering_USB2E57541_16-54-33-159.txt"); %
x_w_RB1 = w_RB1(:,1);
y_w_RB1 = w_RB1(:,2);
w_RB2 = load("Lab1_Milk_Scattering_USB2E57541_16-54-53-603.txt"); %
x_w_RB2 = w_RB2(:,1);
y_w_RB2 = w_RB2(:,2);
% Produce plots of the spectra overlaid in the same figure
plot(x_wo_RB, y_wo_RB, x_w_RB1, y_w_RB1, x_w_RB2, y_w_RB2);
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!