SpaceKidd_N7
SpaceKidd_N7

Reputation: 21

How to save a string variable to a .txt file using MATLAB?

I'm trying to make a log of toc values that are labeled. Here's how I've been trying to accomplish that:

toc
A = 'link_raw_data:' 
B = toc
C = num2str(B)
D = append(A, ' ', C, 's')
save('elapsed_time_log.txt', 'D', '-ASCII');

What should be written to the text file is:

link_raw_data: 304.567s

But instead what's written is:

   1.0800000e+02   1.0500000e+02   1.1000000e+02   1.0700000e+02   9.5000000e+01   1.1400000e+02   9.7000000e+01   1.1900000e+02   9.5000000e+01   1.0000000e+02   9.7000000e+01   1.1600000e+02   9.7000000e+01   5.8000000e+01   3.2000000e+01   5.1000000e+01   4.8000000e+01   5.2000000e+01   4.6000000e+01   5.3000000e+01   5.4000000e+01   5.5000000e+01   1.1500000e+02

I'm not sure what the problem is, could it be because the save function can't save variables that have string values? Is there any alternative function I could use? I tried using fopen together with fprintf but, that didnt even write anything to the text file.

Thank you for your time

Upvotes: 1

Views: 718

Answers (1)

SpaceKidd_N7
SpaceKidd_N7

Reputation: 21

I figured out how to do it:

tic
1st_function
toc 

A = '1st_function: %s\r\n'
B = toc
C = num2str(B)

tic
2nd_function
toc

D = '2nd_function: %s\r\n'
E = toc
F = num2str(E)

file_id = fopen('log.txt', 'w')
fprintf(file_id, A, C)
fprintf(file_id, D, F)

Upvotes: 1

Related Questions