0

For example my data is:

data = 

[1000] @(x)x.^2  @sin [0.5]
[2000] @(x)1./x  @cos [0.6]

I want to save data to a text file or another. (data is a cell matrix). How can I achieve this?

newzad
  • 706
  • 1
  • 14
  • 29
  • Can't you just save `data` in a `mat` file using [`save`](http://www.mathworks.com/help/matlab/ref/save.html)? – p8me Jun 12 '13 at 23:28
  • @natan I have tried sprintf, printf, save; but I didn't get what I desired. Function handles are making trouble. – newzad Jun 12 '13 at 23:35
  • @pm89 I tried. but gedit can not open the mat file. I tried to save as ascii, then the file is empty. – newzad Jun 12 '13 at 23:36

3 Answers3

1

If you want to save data for later usage withing Matlab all you need is this

save('filename','variables separated by spaces'); % to save specific variables
save('filename'); % to save all variables

if you want to load the variables to the workspace again, use the following

load('filename');

if you need to write data as a readable text file instead of binary data, then try to use fprintf, almost usable the same way as C's fprintf. I advise you to check the documentation

Here's a little example :

name = 'John';
age = 20;
enter code here
file = fopen('yourfilename.txt','w') % w option stantds for 'write' permission
fprintf(file,'My name is %s and I am %d', name, age);
fclose(file); % close it when you finish writing all data

I didn't really understand how is your data matrix formatted. It doesn't seem to be correct matlab code.

Regards ;)

ARMBouhali
  • 300
  • 2
  • 12
1

If you want to open it via gedit later you can use evalc to get the exact string you see in command window when you type data:

str = evalc('data');

Then write it to file using fopen and fwrite:

fid = fopen('data.txt', 'w');
fwrite(fid, str);
fclose(fid);
p8me
  • 1,840
  • 1
  • 15
  • 23
1

To get the string representation of an anonymous function use char:

S = cell(size(data,1),1);
for iRow = 1:size(data,1)
    S{iRow}=sprintf('%d %s %s %d\n', ...
            data{iRow,1}, char(data{iRow,2}), char(data{iRow,3}), data{iRow,2}); 
end

and then write S to the output file.

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52