From: us on
"ben harper" <controlusc(a)gmail.com> wrote in message <hss1m1$1to$1(a)fred.mathworks.com>...
> Walter Roberson <roberson(a)hushmail.com> wrote in message <hss18c$ole$1(a)canopus.cc.umanitoba.ca>...
> > ben harper wrote:
> >
> > > I want to convert a mat file to text file
> > > but when i convert with this code:
> > >
> > > clc
> > > clear
> > > load('file.mat')
> > > Conten=who;
> > > save('file.txt',Conten{:},'-ascii')
> > >
> > > It saves only values.
> > > It doesn't save variable names.
> > > How can i save it?
> >
> > Do you want to save *only* the variable names, or *only* the values, or do you
> > want to save both? If both, then how should the names appear relative to the
> > values?
> >
> > You are probably going to need to use fprintf(), but if your variables might
> > contain cell arrays or structures or objects or symbolic objects or function
> > handles or inline functions, then outputting those can get a little messy.
>
> my values are scalar or vectoral.
> there is not any cell array or structure.
>
> mat file has both the variable names and values.
> i want to save them to a text file like:
>
> mass = 12
> velocity= 4

one of the solutions
- for this particulr scenario...

% the data
a=pi;
bb=12;
ccc=1:4;
% the engine
fnam='foo.mat'; % <- your tmp file name...
vn=who('ccc','bb','a'); % <- or ...who... if you start with a clean ws...
save(fnam,vn{:});
s=load(fnam);
ix=num2cell(1:numel(vn)).';
vv=struct2cell(s);
r=cellfun(@(x,y) [sprintf('%s = ',vn{x}),sprintf('%g ',y)],ix,vv,'uni',false);
% the result
disp(r);
%{
'a = 3.14159 '
'bb = 12 '
'ccc = 1 2 3 4 '
%}

us
From: ben harper on
"us " <us(a)neurol.unizh.ch> wrote in message <hss4g0$8j6$1(a)fred.mathworks.com>...
> "ben harper" <controlusc(a)gmail.com> wrote in message <hss1m1$1to$1(a)fred.mathworks.com>...
> > Walter Roberson <roberson(a)hushmail.com> wrote in message <hss18c$ole$1(a)canopus.cc.umanitoba.ca>...
> > > ben harper wrote:
> > >
> > > > I want to convert a mat file to text file
> > > > but when i convert with this code:
> > > >
> > > > clc
> > > > clear
> > > > load('file.mat')
> > > > Conten=who;
> > > > save('file.txt',Conten{:},'-ascii')
> > > >
> > > > It saves only values.
> > > > It doesn't save variable names.
> > > > How can i save it?
> > >
> > > Do you want to save *only* the variable names, or *only* the values, or do you
> > > want to save both? If both, then how should the names appear relative to the
> > > values?
> > >
> > > You are probably going to need to use fprintf(), but if your variables might
> > > contain cell arrays or structures or objects or symbolic objects or function
> > > handles or inline functions, then outputting those can get a little messy.
> >
> > my values are scalar or vectoral.
> > there is not any cell array or structure.
> >
> > mat file has both the variable names and values.
> > i want to save them to a text file like:
> >
> > mass = 12
> > velocity= 4
>
> one of the solutions
> - for this particulr scenario...
>
> % the data
> a=pi;
> bb=12;
> ccc=1:4;
> % the engine
> fnam='foo.mat'; % <- your tmp file name...
> vn=who('ccc','bb','a'); % <- or ...who... if you start with a clean ws...
> save(fnam,vn{:});
> s=load(fnam);
> ix=num2cell(1:numel(vn)).';
> vv=struct2cell(s);
> r=cellfun(@(x,y) [sprintf('%s = ',vn{x}),sprintf('%g ',y)],ix,vv,'uni',false);
> % the result
> disp(r);
> %{
> 'a = 3.14159 '
> 'bb = 12 '
> 'ccc = 1 2 3 4 '
> %}
>
> us

Thank you Walter, Thank you us