write table into txt file using writetable returns error
Ältere Kommentare anzeigen
i have this in my main, and its return a table called 'step2_result_euclidean' contains a table. This function may be used fews time and each being called i want to save it into a txt file for monitoring
for ccc=1:k
step2_euclidean = kira_euclidean(data,step1_result_centroid,ccc);
result_step2=array2table(step2_euclidean);
step2_result_euclidean(:,ccc)=result_step2(:,end) ;
end
push_result(step2_result_euclidean);
This is my push_result function code:
function[]= push_result(data_to_be_push)
writetable(data_to_be_push,'result.dat''Delimiter',' ')
However, i get this error:
Error using writetable (line 124)
Wrong number of arguments.
How to fix this error? TQIA
Antworten (1)
Cris LaPierre
am 4 Feb. 2021
It looks like you have a syntax error when you call writetable. You need to separate each input with a comma. You are missing a comma between your file name and the name of your Name-Value pair. Try this.
function[]= push_result(data_to_be_push)
writetable(data_to_be_push,'result.dat','Delimiter',' ')
11 Kommentare
Khairul nur
am 4 Feb. 2021
Walter Roberson
am 4 Feb. 2021
Is each iteration to store to a different file, or should each iteration to append to the end of the file?
Khairul nur
am 4 Feb. 2021
Walter Roberson
am 4 Feb. 2021
writetable() has an writemode append option for text files.
Khairul nur
am 4 Feb. 2021
Bearbeitet: Khairul nur
am 4 Feb. 2021
Cris LaPierre
am 4 Feb. 2021
If you can't update your version, then perhaps you should look into low-level file i/o. This is actually what writetable is using to write the data. Use fopen with the 'a' permission to append data to the file. You then use fwrite to write the data, one line at a time, to the file. Then use fclose to close the file.
Here is a template (untested). It assumes the table only contains numeric data.
function[]= push_result(data_to_be_push)
fid = fopen('result.dat','a');
fwrite(fid,data_to_be_push{:,:},'double');
fclose(fid);
end
Walter Roberson
am 4 Feb. 2021
fwrite() is for writing in binary, which the user probably does not want to do (they used 'delimiter' option, which is only for text.)
For text writing, you would want fprintf. Assuming that data_to_be_push is a table of numeric data, then
function[]= push_result(data_to_be_push)
if isempty(data_to_be_push); return; end
filename = 'result.dat'; %but in which directory??
[fid, message] = fopen(filename,'a');
if fid < 0
error('failed to open file "%s" because: "%s"', filename, message);
end
fmt = [repmat('%.18g ', 1, size(data_to_be_pushed,2)-1), '%.18g\n'];
fprintf(fid, fmt, data_to_be_push{:,:}.'); %transpose is important!
fclose(fid);
end
Here I use a .18g format, whereas you are probably more accustomed to seeing .16g . Roughly 1/5 of floating point numbers need .17g instead of .16g to fully reproduce, and I recently saw argument that .18g is needed in some borderline cases (that end in multiple 9's). I am sure that .17g is needed, not certain that .18g is needed.
Cris LaPierre
am 4 Feb. 2021
Ha. Well, I did say it was untested. Yes, fprintf is a better choice.
Khairul nur
am 4 Feb. 2021
Bearbeitet: Khairul nur
am 4 Feb. 2021
Walter Roberson
am 4 Feb. 2021
fmt = [repmat('%.18g ', 1, size(data_to_be_push,2)-1), '%.18g\n'];
I had an error in the variable name, as 'data_to_be_pushed' is more natural than 'data_to_be_push'
Khairul nur
am 5 Feb. 2021
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!