I have a .txt file that I need to turn into a vector

9 vues (au cours des 30 derniers jours)
Will
Will le 6 Oct 2022
My .txt file is just "2 4 3 4". I need to turn this into a vector but I dont know how.
I loaded my .txt file into the document by going Grades = load("Grades.txt"); then I tried to make that into a vector by doing CourseGrade = linspace("Grades") but it didnt work. It wants me to put real numbers in there.
Please help me out, thanks!

Réponses (2)

dpb
dpb le 6 Oct 2022
v=readmatrix('YourFile.txt');

Walter Roberson
Walter Roberson le 6 Oct 2022
filename = 'Grades.txt';
%first create an example file
example_data = "2 4 3 4";
writelines(example_data, filename);
dbtype(filename)
1 2 4 3 4
%now do the work
Grades = load(filename)
Grades = 1×4
2 4 3 4
%Grades is now a row vector because that is what was in the file.
%if you need it to be a column vector then transpose it
Grades = Grades.'
Grades = 4×1
2 4 3 4

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by