Selection sort script - problem

Hello, I made a script in MATLAB and it works perfectly, but when I try to make a Selection sort scrip in Octave, im getting:

"error: scalar cannot be indexed with .

error: called from

s at line 53 column 14

SortSelection at line 61 column 14"

Here is a code:

array = randi([1,20],1,20);

index = 1:size(array,2);

figure(1)

cla

hold on

points = bar(index,array,‘FaceColor’,‘b’);

axis off

ylim([0,max(array)]);

function array = s(array,mode,points)

n= size(array,2);

for i = 1:n

min_ind = i;

for j = i+1:n

if mode == “ascend”

if array(min_ind) > array(j)

min_ind = j;

endif

elseif mode == “descend”

if array(min_ind) < array(j)

min_ind = j;

endif

endif

endfor

temp = array(i);

array(i) = array(min_ind);

array(min_ind) = temp;

points.YData = array;

drawnow()

endfor

endfunction

sorted_array = s(array,“ascend”,points);

Please! If you must paste code, attach it or enclose it in triple backquotes otherwise the formatting is a mess like what you posted. Even the single and double quotes were messed up.

I’ll take it on faith you’re trying to create a visualization of different sorting techniques. The flaw is in your trying to change the YData of points with points.YData = array;. The variable points is only a scalar, representing a single handle. Calling it with a struct notation will cause an error as you saw. What you meant is this:

set (points, "YData", array);

Try that instead and you will see an animation as you probably intended.

Edit: BTW, you don’t want to use == to compare the sorting mode to “ascend” or “descend”, because those are different lengths – change “ascend” to “descend” in the last line and you’ll trigger an error about a length mismatch. Try strcmp, strncmp etc. Also, as best practice, don’t name your variables index or mode because those are preexisting functions in Octave and it will lead to errors later that are difficult to debug, like if you forget you had a variable mode and try to use it in its statistical meaning.