How do i get my code to read the updated csv table instead of the old table?

1 Ansicht (letzte 30 Tage)
Hello guys, new to mathlab guy here
So I have a little problem with a little program im trying to make for a course im doing. The idea with the program is to make a program that is able to read a CSV table matrix, find invalid grades and then correct them directly in the CSV file (from within matlab). However after I use my first edit('grades.csv') and manually correct the invalid grade to a valid grade it still detects the grade as an indvalid one. It may be easier to see mistake in the code. The csv file should be attached
I know the code is not very optimized yet, it is still in the early stages.
function data
data = readtable('grade.csv');
studentName = data{:, 2};
[rownum, column] = size(data);
A = data;
A(:, 1:2) = [];
A = table2array(A);
choiceG = menu('Choose one option', 'Check for valid grade range', 'Finish');
while true
if choiceG == 1
for i = 1:rownum
for k = 1: size(A,2)
if all(ismembertol(A(i,k), [-3, 0, 2, 4, 7, 10, 12]))
%do something
else
choiceGrade = menu(sprintf('Grade assignment: %g for student: %s is not a valid grade \nwould you like to correct the error?',k, string(studentName(i))), 'yes', 'no');
if choiceGrade == 1
edit('grade.csv') % Here i edit the csv file
elseif choiceGrade == 2
choiceG = menu('Choose one option', 'Check for valid grade range', 'Finish'); % just a return to the menu
end
end
end
end
end
if choiceG == 2
break
end
end
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Jan. 2020
You need to assign A(i, k) the corrected value. Then, after the loop once, all A(i, k) have been inspected and corrected (if needed), you need to call writetable() or csvwrite().

Weitere Antworten (1)

dpb
dpb am 20 Jan. 2020
It would be better to follow IA's suggestion of fixing the grades in memory and updating the file, but...
Your logic error here is that you're edting the original file on disk but then after the user has done that you haven't refreshed the table in memory by re-reading the file.
It could be done that way, by by doing that you let the user make any changes whatsoever to anything in the file and have no way to catch typos (or even worse, deliberate mischief to try to trip you up :) ). If you let the user only select from a list of possible grades for a bad element and fix it in the memory copy of the file as he suggests, then you write the new file after all done and can be sure all at least are valid entries (whether they're the correct grades for the given student is a different problem!).
  1 Kommentar
William Nielsen
William Nielsen am 20 Jan. 2020
Thanks for the tip! I didnt take account for that people could accidentally type a comma or otherwise. I ended up doing as IA suggested and it worked :).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by