Dear all,
I'm a newbie to Matlab programming and need some help. I wrote a program to sort the input data in ascending order. However, the functions did not work properly. Can you please shed some light? Thanks heaps.
Oliver.
nvals =input('Enter number of values in the data set: ');
array = zeros(nvals,1);
sorted = zeros(nvals,1);
for ii = 1:nvals
string = ['Enter value ' int2str(ii) ': '];
array(ii) = input(string);
end
sorted = ssort(array);
fprintf('\nSorted data: \n');
for ii = 1:nvals
fprintf(' %8.4f\n', sorted(ii));
end
function out = ssort(a)
for i=1:nvals
if array(i)>array(i+1)
sorted(i) = array(i+1);
sorted(i+1) = array(i);
array(i+1) = array(i);
else
sorted(i) = array(i);
end
end
out = a;
*Errors are as follows: ??? Undefined function or method 'sorted' for input arguments of type 'double'.
Error in ==> test_ssort at 11 fprintf(' %8.4f\n', sorted(ii));
??? Error: File: test_ssort.m Line: 14 Column: 1 Function definitions are not permitted in this context.
??? Error: File: test_ssort.m Line: 17 Column: 1 Function definitions are not permitted in this context.
??? Error: File: test_ssort.m Line: 17 Column: 1 Function definitions are not permitted in this context.
??? Error: File: test_ssort.m Line: 18 Column: 1 Function definitions are not permitted in this context.
??? Error: File: test_ssort.m Line: 18 Column: 1 Function definitions are not permitted in this context.*