A problem with a code that works as it is but gives an index error when is used inside a function
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I am writting a code that basically reads force curves from my microscope and then auto-align them based on their baseline and the offset. After it does it, I then divide the data into 30 graphs blocks so that I can remove bad curves. I basically 113 curves so there are 4 blocks where the last block has only 23 curves.
The problem is that when I run the code by itself it works perfectly. However when I put it inside a function, I am getting when it enter the 4th block the following error message: "Index in position 2 exceeds array bounds. Index must not exceed 100. Error in cleaner (line 118) z_corrj = z_corr(:, j);" - I just can't understand why it works perfectly but crashes when it is part of the function.
Here is the original code:
block_size = 30;
% Calculate the number of blocks
num_blocks = ceil(size((d_corr),1) / block_size)
% Loop through each dataset
for i = 1:num_blocks
figure;
hold on;
start_idx = (i-1) * block_size + 1;
end_idx = min(i*block_size, size((d_corr),1));
% Extract the i-th column for X and Y
for j = start_idx:end_idx
z_corrj = z_corr(:, j);
d_corrj = d_corr(:, j);
% Assign a constant Z value for this dataset
Zj = ones(size(z_corrj)) * j;
% Plot using plot3
plot3(z_corrj, d_corrj, Zj);
end
% Customize the plot
xlabel('Z [nm]');
ylabel('Deflection [V]');
zlabel('Index');
title('3D Plot of 10 XY sets');
grid on;
% Set the view orientation
azimuth = 117.4; % Azimuth angle in degrees
elevation = 50; % Elevation angle in degrees
view(azimuth, elevation);
hold off;
%Remove bad curves
trash = 0;
trash = input('If you want to remove a curve, input its number x (press Enter if you are done): ');
if trash
z_corr(:,trash) = [];
d_corr(:,trash) = [];
else
end
if i<num_blocks
disp('Press Enter to display the next block') ;
pause;
end
end
0 comentarios
Respuestas (1)
Voss
el 17 de Jul. de 2024
Based on the error message, "Index in position 2 exceeds array bounds. Index must not exceed 100. Error in cleaner (line 118) z_corrj = z_corr(:, j);" we know that z_corr has 100 columns. And since you say you have 113 curves, I'll assume that each curve corresponds to a row of z_corr, so that z_corr has 113 rows. I'll also assume d_corr and z_corr have the same size.
If those assumptions are correct, then the problem is that you are taking the jth column of z_corr and d_corr here
z_corrj = z_corr(:, j);
d_corrj = d_corr(:, j);
when you should be taking the jth row
z_corrj = z_corr(j, :);
d_corrj = d_corr(j, :);
because curves correspond to rows. Your num_blocks and end_idx are based on the number of rows of d_corr (size(d_corr,1)), so you need to take a row from each matrix, not a column.
Making that change (and also commenting out the user-interaction section for running in this environment - and fixing the plot titles to correctly indicate the number of curves while I'm at it) the code runs without error, using random 113-by-100 matrices for d_corr and z_corr:
d_corr = rand(113,100);
z_corr = rand(113,100);
block_size = 30;
% Calculate the number of blocks
num_blocks = ceil(size((d_corr),1) / block_size)
% Loop through each dataset
for i = 1:num_blocks
figure;
hold on;
start_idx = (i-1) * block_size + 1;
end_idx = min(i*block_size, size((d_corr),1));
% Extract the j-th row for X and Y
for j = start_idx:end_idx
z_corrj = z_corr(j, :);
d_corrj = d_corr(j, :);
% Assign a constant Z value for this dataset
Zj = ones(size(z_corrj)) * j;
% Plot using plot3
plot3(z_corrj, d_corrj, Zj);
end
% Customize the plot
xlabel('Z [nm]');
ylabel('Deflection [V]');
zlabel('Index');
title(sprintf('3D Plot of %d XY sets',end_idx-start_idx+1));
grid on;
% Set the view orientation
azimuth = 117.4; % Azimuth angle in degrees
elevation = 50; % Elevation angle in degrees
view(azimuth, elevation);
hold off;
% %Remove bad curves
% trash = 0;
% trash = input('If you want to remove a curve, input its number x (press Enter if you are done): ');
% if trash
% z_corr(:,trash) = [];
% d_corr(:,trash) = [];
% else
%
% end
% if i<num_blocks
% disp('Press Enter to display the next block') ;
% pause;
% end
end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!