write table into txt file using writetable returns error

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
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

tq for correcting my syntax error, the code is run..
however, the txt file store only the latest table generated by the loop..it suppose to store table generated by each loop
control_loop==0
while control_loop<10
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);
control_loop=control_loop+1
end
Is each iteration to store to a different file, or should each iteration to append to the end of the file?
each iteration to append in the same file
writetable() has an writemode append option for text files.
sadly not supported by R2019b as it is my version :( Correct me if i am wrong
i edit my code become :
function[]= push_result(data_to_be_push)
data=data_to_be_push
writetable(data_to_be_push,'result.dat','WriteMode','append ')
has error:
Error using writetable (line 124)
Invalid parameter name: WriteMode.
Error in push_result (line 4)
writetable(data_to_be_push,'result.dat','WriteMode','append ')
Any other solution for my problem?
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
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.
Ha. Well, I did say it was untested. Yes, fprintf is a better choice.
tq Cris and Walter, i appreciated ur reply :) However i still facing error. Please help me to solve this.
Btw walter, i trying to understanding on 18g and 17g and all as i the code as below error:
fmt = [repmat('%.16g ', 1, size(data_to_be_pushed,2)-1), '%.16g\n'];
since i try to play around and changing your code using '16g' as below code:
function[]= push_result(data_to_be_push)
if isempty(data_to_be_push); return; end
filename = 'C:\Users\Khairul Nurmazianna\OneDrive\Desktop\FCM\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('%.16g ', 1, size(data_to_be_pushed,2)-1), '%.16g\n'];
fprintf(fid, fmt, data_to_be_push{:,:}.'); %transpose is important!
fclose(fid);
end
This is the example of data_to_be_pushed (table) looks like, each iteration will produce the same size of table.
1.46995037518577 0.685711409618569 0.627605582262098
1.80125654902813 2.11239700592433 2.40486047110991
4.96690963594932 5.82573980407720 6.29348791553382
10.1325627228705 11.5390826022301 12.1821153599577
0.884083765656048 0.642946391309453 0.498325558636135
4.38104302641960 5.78297478576809 6.16420789190786
2.29821715612633 2.60018137300034 2.36904553501017
1.46387024304751 2.31352417115322 2.25767297943408
9.54669611334078 11.4963175839210 12.0528353363318
3.22147007163667 2.44181922608056 2.64551305031197
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'
hihi..ok..the variable 'pushed'..i overlook on this as trying so hard to understand repmat function 18g or 17g..and its work! ur are great in matlab Mr. Walter and helpfull too.. :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Gefragt:

am 4 Feb. 2021

Kommentiert:

am 5 Feb. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by