17 visualizzazioni (ultimi 30 giorni)

Mostra commenti meno recenti

Itay il 17 Lug 2024 alle 13:20

Risposto: Voss il 17 Lug 2024 alle 14:57

Apri in MATLAB Online

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 Commenti Mostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Mostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Accedi per rispondere a questa domanda.

### Risposte (1)

Voss il 17 Lug 2024 alle 14:57

⋮

Apri in MATLAB Online

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)

num_blocks = 4

% 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 Commenti Mostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Mostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Accedi per rispondere a questa domanda.

### Vedere anche

### Tag

- index error message

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

**Si è verificato un errore**

Impossibile completare l'azione a causa delle modifiche apportate alla pagina. Ricarica la pagina per vedere lo stato aggiornato.

Seleziona un sito web

Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .

Puoi anche selezionare un sito web dal seguente elenco:

Americhe

- América Latina (Español)
- Canada (English)
- United States (English)

Europa

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia-Pacifico

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contatta l’ufficio locale