Charles Gao
Charles Gao

Reputation: 679

How to save a matlab source code into a string in matlab?

I want to save a matlab source code into a string format in matlab. Does anyone know how to do this? For example,

type xxx.m

can display the source code of xxx.m. Then using what command am I able to save it into a string?

Upvotes: 1

Views: 460

Answers (2)

Eitan T
Eitan T

Reputation: 32930

Two approaches to this that I can think of are:

  1. Storing the output of type filename into a string using evalc, for example:

    str = evalc('type filename');
    
  2. Directly reading the file and storing its contents into a string, for instance:

    C = textread(filename, '%s', 'delimiter', '');
    str = sprintf('%s\n', C{:});
    

    There are, of course, alternative ways of doing this with textscan, fgets, fgetl, etc...

The resulting str should now hold the contents of your file.

Upvotes: 3

Suedocode
Suedocode

Reputation: 2524

fid=fopen('filename.whatever','r')
txt=fread(fid,'uint8=>char')' %note the transpose!

Upvotes: 0

Related Questions