Read strings from a text file and pass it to variables

84 vues (au cours des 30 derniers jours)
Mini Me
Mini Me le 6 Fév 2013
I have this code in matlab that seems to read the content of my file
fid = fopen('file.txt');
tline = fgets(fid); while ischar(tline) disp(tline) tline = fgets(fid);
end
fclose(fid);
what I have Included in the file are inputs. they are align in the file in that format
20
40
30
30
Is there a way to grab them one by one and assign them to a variable that is
part of a function such as function [lala]=myfunc(t,k,l,m)

Réponses (2)

TAB
TAB le 6 Fév 2013
Modifié(e) : TAB le 6 Fév 2013
fid = fopen('file.txt');
% Read all lines & collect in cell array
txt = textscan(fid,'%s','delimiter','\n');
% Convert string to numerical value
Val = str2double(txt{1});
t = Val(1);
k = Val(2);
l = Val(3);
m = Val(4);
% Call your function
[lala]=myfunc(t,k,l,m);
  1 commentaire
Mini Me
Mini Me le 6 Fév 2013
You ,sir,are a life saver. thank you!

Connectez-vous pour commenter.


Jan
Jan le 6 Fév 2013
What about:
fid = fopen('file.txt');
A = fscanf(fid, '%g', Inf);
fclose(fid);

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by