MATLAB: Storing elments of marix from text file into variables

indexingloadmatrixmatrix arraytext file

I'm having one text file containing a matrix in which the first column is a variable lets say x ,first row is a variable lets say y and all the elements inside except x and y are z.How do I read this text file and store them in separate variables .I tried using this but it didn't work.I think I've not stored the elements in z correctly if yes what should I do to correct it?
input = load('matrix.txt');
x= input(:,1)./100; y= input(1,:); z =input(:,:);
end

Best Answer

This is a bad method to store data, because Matlab must guess the size of the data.
fid = fopen(FileName, 'r');
if fid==-1, error(Cannot open file: %s', FileName); end
FirstLine = fgets(fid);
y = sscanf(FirstLine, '%f');
n = length(y);
M = fscanf(fid, '%f', [n+1, Inf]);
fclose(fid);
x = M(:, 1);
Data = M(:, 2:n+1);
Perhaps you need some transpose operators to get the wanted orientations.
Related Question