Storing elments of marix from text file into variables
Afficher commentaires plus anciens
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
2 commentaires
Is that matrix correct? It seems to be missing the first element. Could you confirm if the first row would have four or five elements.
Because of the missing first element there are different ways that this matrix could be read into MATLAB.
Mohammad Zaved Siddiqui
le 21 Sep 2015
Modifié(e) : Mohammad Zaved Siddiqui
le 21 Sep 2015
Réponse acceptée
Plus de réponses (1)
Thorsten
le 21 Sep 2015
Based on Jan's solution, I wrote the following function that does the job:
function [x y data] = readxydata(filename)
fid = fopen(filename, 'r');
if fid == -1, error('Cannot open file: %s', filename); end
y = sscanf(fgets(fid), '%f');
data = fscanf(fid, '%f', [numel(y)+1, Inf])';
st = fclose(fid);
if st ~= 0, error('Cannot close file: %s', filename); end
x = data(:, 1);
data = data(:, 2:end);
Catégories
En savoir plus sur Cell Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!