Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Save strings and numbers together
Replies: 4   Last Post: Mar 14, 2013 9:53 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Phil

Posts: 61
Registered: 12/8/08
Re: Save strings and numbers together
Posted: Mar 14, 2013 8:14 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Phil Roberts" wrote in message <khse66$h8a$1@newscl01ah.mathworks.com>...
> "Bruce " <blmckinley@signalprocessing.com> wrote in message <khprk6$c5d$1@newscl01ah.mathworks.com>...
> > "Phil Roberts" wrote in message <khpl27$nat$1@newscl01ah.mathworks.com>...
> > > Hi,
> > >
> > > I have the following strings and number as separate variables. How can I save them together in one file, such as a .txt file?
> > >
> > > Strings
> > > 'Test Point'
> > > 'speed_Engine_av'
> > > 'torque_Brake_av'
> > > 'power_Brake_av'
> > >
> > > Numbers
> > > 1
> > > 2
> > > 3
> > > 4
> > >
> > > I wish to save them so that the string and its corresponding number are on the same row i.e. Test Point 1, speed_Engine_av 2 etc
> > >
> > > Thanks in advance,
> > >
> > > Phil

> >
> > As long as the strings don't include any commas, you can use a Comma Separated Value file. You can open the file as a spreadsheet and the cells will appear in separate columns. Or you can open it and fscanf your values back into MATLAB or using any other programming language.
> >
> > strvar = {'Test Point', 'speed_Engine_av'. 'torque_Brake_av', 'power_Brake_av'};
> > val = {1, 2, 3, 4};
> > fid=fopen('myfile.csv','wt');
> > for i=1:length(val)
> > fprintf(fid,'%s, %d\n',strvar{i},val{i});
> > end
> > fclose(fid);

>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> Thanks for the response.
>
> If I have I have an additional column of numbers in the "val" parameter, how to I add this to fprintf so both columns of val are printed in the file? I am a new user of matlab so apologies for the simipicity of this question.
>
> Thanks in advance,
>
> Phil


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Sorry, I have sorted my question above.

However, I have another question.

How do I increase the number of output columns in fprintf if "val" is changing each time? For example:
For 1 column in val; fprintf(fid,'%s %5.3f\n',strvar{i},val(i,:))
For 2 column in val; fprintf(fid,'%s %5.3f %5.3f\n',strvar{i},val(i,:))
For 3 column in val; fprintf(fid,'%s %5.3f %5.3f %5.3f\n',strvar{i},val(i,:))

I would like to add the %5.3f for each additional column in val automatically.

Many thanks in advance,

Phil



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.