How can I put the table in txt?

1 view (last 30 days)
Hyeonjin Hwang
Hyeonjin Hwang on 3 Jan 2019
Commented: Cris LaPierre on 4 Jan 2019
I'm currently using the push button gui to display the time, each time I click.
I made this into a table now and saved it to a text file. As follows
if (get (handles.pushbutton1, 'Value'))
time1 = clock;
end
S.clock = datestr (time1);
T = struct2table (S);
assignin ('base', 'S', S);
assignin ('base', 'T', T);
fid = fopen ('clock.txt');
if fid <0
write (T, 'clock')
else
fid = fopen ('clock.txt', 'a +');
fprintf (fid, 'What should I put here?', T);
fclose (fid);
end
But how can I save the time vlaue to 'clock.txt' everytime pushbutton is pressed. In other words, I hope looks like this when I open a text file.
clock
'Time when first pressed'
' Second time pressed '
.
.
.
'nth time pressed'
I have used fopen to store Time when nth pressed in a text file.
And I have confirmed that the code is alright until fid <0 .
What should I put between fid and T in fprintf? I would appreciate your help.
  2 Comments
Bob Thompson
Bob Thompson on 3 Jan 2019
Why are you opening the file twice?
What is the class of T? You can find out by entering >> class T
Hyeonjin Hwang
Hyeonjin Hwang on 4 Jan 2019
I didn't notice that I called the table twice. So I deleted one.
And the class of T is the char.
Thanks.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 4 Jan 2019
I'm pretty sure fprintf can't write a table. Try using writetable instead.
  5 Comments
Walter Roberson
Walter Roberson on 4 Jan 2019
numeric_to_write = randi(255);
fprintf(fid, '%s %d\r\n', S.clock, numeric_to_write);
Cris LaPierre
Cris LaPierre on 4 Jan 2019
Is there something we are missing? The code shared is for writing the values to a file. If you need a table for other parts of your code, feel free to add them. They just aren't necessary for writing a text file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!