Search this blog!

Friday, February 24, 2012

In Matlab : Sorting by indexes of another sorting

Imagine, we have the following two array of numbers
A=[ 2 4 6 7 8 8];
 B=[ 5 8 2 5 0 5];
Problem:
---------
we want to sort A and then arrange B according to the index of A.

Solution:
----------

>> [sortedA,indexA]=sort(A)

sortedA =

     2     4     6     7     8     8

indexA =

     1     2     3     4     5     6



>> B(indexA)

ans =

     5     8     2     5     0     5

Thursday, February 16, 2012

How to append in a excel file using Matlab?

Imagine that I have the following excel file called "algoperformace.xls"


algoName mode 95percentile max mean
Algorithm1 13.15153679 20.03323188 39.52043729 15.65858


Now, I would like to add a new line as the following into this excel file using matlab


Neighbor 2.721678458 5.536962451 26.63633753 3.858783


How to do that?
----------------------------------------------------------------------------------
Answer:
%% Loading the excel into matlab

[~, ~, alldata] = xlsread('algperformace.xls');
% We will add new entry line into 'alldata' variable and then will write the excel file
% The script will compute the value for algoname, mode, color_diff_95_percentile, max, mean
% adding those values into the cell of 'alldata'
alldata(size(alldata,1),:)={algoname, mode, color_diff_95_percentile, max, mean};
%writing the complete new 'alldata' into the same excel file
xlswrite('algperformace.xls', alldata);

Try your own example :)


Thursday, February 2, 2012

How may I make the user to browse and select file in Matlab


Very often, Matlab programs need to specify input files or images. Instead of giving the absolute path to that file we may make the user to browse and locate the file.

uigetfile is the function that can do that job. Here is an example.

disp('Select the Reference view');
[filename_ref,pathname_ref] = uigetfile('*.*','Select the Reference view');
if isequal(filename_ref,0)
    disp('User selected Cancel')
else
    disp(['Reference view: ', fullfile(pathname_ref, filename_ref)])
end