How do i calculate the sum of each row
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
fid = fopen('studentmarks.txt', 'r')
if fid == -1
disp('Error, check file name')
else
T = textscan(fid,'%s %s %f %f %f %f %f %f %f %f')
end
fclose(fid)
0 Kommentare
Akzeptierte Antwort
dpb
am 31 Jul. 2021
I'd suggest to use readtable instead, first...
T = readtable('studentmarks.txt');
T.TotalMarks=sum(T{:,3:end},2);
presuming the above format string is correct for the file content.
Without a header line in the file, the default variable names will be Var1 through VarN, you can make use of meaningful names by assigning them in T.Properties.VariableNames.
See the doc on the table class for all the syntax...
The Q? answer specifically is to use sum() with the second optional input argument to tell it to sum rows instead of columns.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!