From: James Tursa on
"Lei Liew" <lei.liew(a)gmail.com> wrote in message <hlkklc$gcl$1(a)fred.mathworks.com>...
> Dear all,
>
> I am a beginning Matlab user and trying to do a relatively simple task. I have a series of vectors saved in a file ('vectors.m'). I want to create a new file ('vectors2.m') that is a copy of the first, and search for one vector ('vectorB') and randomly sort the values into two groups (e.g. 'vectorB1' and 'vectorB2'), in the end replacing 'vectorB' with 'vectorB1' and 'vectorB2'.
>
> The format of the 'vectors.m' file is such:
> vectorA = [1 2 3 4 5];
> vectorB = [1 2 3 4 5 6 7];
> vectorC = [1 2 3 4]; and so on.
>
> So far what I have is the following:
>
> copyfile('vectors.m', 'vectors2.m');
> newfile = 'vectors2.m';
> fid = fopen(newfile,'r+');
>
> Now that I'm trying to read the file values in, I tried both textscan and fscanf and neither seem to be working. I think I am missing something because I keep getting C = []...
>
> Are those two functions only for reading text files, or can they read M files too? Is there an easier way to get vectors defined in an M file into the Matlab workspace?
>
> Thanks very much for your help!
> Best,
> Lei

Files ending in .m are m-files that are functions or scripts meant to be run, not read with textscan etc. What are the *exact* contents of the file vectors.m? Can you post the contents exactly as it appears in the file?

James Tursa