function matrix = read_matrix_file(file_name)
%Reads a matrix from a file where matrix is given in clear format. Means,
%that opening that file with an other text editor you see matrix. Nothing else
%than numbers row by row, column by column. 
%Number of columns is automatically identified.
%
%Input: fully referenced by path: E:\files\thisfile.asc
%Output: m-by-n matrix if valid file, -1 if invalid file or content

fid = fopen(file_name,'r'); 
if fid == -1
    matrix = -1;
    return;
end

first_line = fgetl(fid);
if first_line == -1         %if no first line could be read
    fclose(fid);
    matrix = -1;
    return;
end

frewind(fid);               %set pointer on open file back to beginning
first_line = str2num(first_line);   %analyse first line
k = numel(first_line);      %assume that numel returns number of columns
format_string = [];
for i=1:k
    format_string = strcat('%e ',format_string);    %for reading floating point numbers
end
matrix = fscanf(fid,format_string,[k,inf]);     %%e allows usage of 'inf'
matrix = matrix';       %will be read as horizontically matrix, so transpose
