mersenneforum.org Matlab help....
 Register FAQ Search Today's Posts Mark Forums Read

 2004-04-27, 15:54 #1 dave_0273     Oct 2003 Australia, Brisbane 2×5×47 Posts Matlab help.... Ok, consider the following string s='8621897,dkgraph,P450,WV1,00000000' (straight out of hrf3.txt) Now, we find the the ',' is in position 8. What I want to do now, is create a string of just the exponent, ie a string of positions 1 through 7. What I want to end up with is exponent='8621897' (Then I can just use str2num to get in into a number) What syntax would I need to do this?? I am assuming something along the lines of exponent=[s(1),s(7)] but all variations of this don't work. Or at least the ones that I have tried don't. Ok, up to question number 2. When i output something from matlab into a txt file using fprintf, I then use a carriage return, fprintf('\n') to get it onto the next line. When I then go and look at my txt document, where it should have gone to the next line, it looks as if there is a little box there instead, and everything just carries on that line instead of going to the next line. However, when I then copy the txt file and paste it into a word document, word seems to be able to interpret the 'little boxes' as returns. I then just copy it back into a txt document and the returns stay there. I was wondering though if there was an easier way of doing this?? Or am I just doing something wrong with my syntex??
2004-04-27, 16:47   #2
geoff

Mar 2003
New Zealand

100100001012 Posts

Quote:
 Originally Posted by dave_0273 s='8621897,dkgraph,P450,WV1,00000000'
You could try sscanf (scan from string), something like this:

[exponent, user, machine, program, error] = sscanf(s, '%d,%s,%s,%s,%s')

or you could read from the file using fscanf in a similar way and bypass the intermediate string.

Quote:
 When i output something from matlab into a txt file using fprintf, I then use a carriage return, fprintf('\n') to get it onto the next line. When I then go and look at my txt document, where it should have gone to the next line, it looks as if there is a little box there instead, and everything just carries on that line instead of going to the next line.
Microsoft distinguishes between text and binary modes, use the 't' permission when opening the file for text mode, something like:

file = fopen('myfile.txt', 'wt')

 2004-04-27, 17:53 #3 geoff     Mar 2003 New Zealand 48516 Posts Sorry I was thinking C, the sscanf line should just be exponent = scanf(s, '%d')
 2004-04-27, 19:49 #4 Uncwilly 6809 > 6502     """"""""""""""""""" Aug 2003 101×103 Posts 297316 Posts Check out this thread. Maybe you can talk nice to one of the folk there to make any changes that would suit you....
2004-04-27, 20:04   #5
aaronl

Aug 2003

24·3 Posts

Quote:
 Originally Posted by geoff Microsoft distinguishes between text and binary modes, use the 't' permission when opening the file for text mode, something like: file = fopen('myfile.txt', 'wt')
CRLF was one of the worst mistakes they ever made. But anyway, doesn't it default to text? I thought the 'b' flag was used for binary:

Quote:
 The mode string can also include the letter b'' either as a last character or as a character between the characters in any of the two- character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (ANSI C'') and has no effect; the b'' is ignored on all POSIX conforming systems, including Linux. (Other sys- tems may treat text files and binary files differently, and adding the b'' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)

 2004-04-28, 03:39 #6 dave_0273     Oct 2003 Australia, Brisbane 1110101102 Posts Thanks for your replies. Geoff, that worked wonderfully. However, as always, I now have another question. Consider the following string... string='abcdefghijklmnopqrstuvwxyz' Now, what I want to be able to do is set up a new string with just those characters between positions 12 and 20. (12 and 20 aren't anything special, I am just giving random numbers to work with). So, the answer should be... new_string='lmnopqrst' Any ideas on how I might be able to do this (in matlab)???
2004-04-28, 06:33   #7
Uncwilly
6809 > 6502

"""""""""""""""""""
Aug 2003
101×103 Posts

245638 Posts

Poking arround on the net it looks like you can reference fixed substrings like:
s(12:20)

Here is some text that I found.:
Quote:
 Strings are just lists of characters – e.g. s = 'fgh' or s = '12fgh' Strings are actually stored as arrays of characters and so we can use the length function and refer to individual characters or substrings using array notation. For example if s = 'abcdefgh' length(s) = 7 s(3) refers to the 3rd entry in s and gives 'c' s(3:5) refers to the characters from 3rd to 5th position and gives 'cde'

 2004-04-28, 06:39 #8 dave_0273     Oct 2003 Australia, Brisbane 1110101102 Posts Thanks uncwilly - that worked perfectly!
2004-04-28, 14:24   #9
geoff

Mar 2003
New Zealand

13·89 Posts

Quote:
 Originally Posted by aaronl CRLF was one of the worst mistakes they ever made. But anyway, doesn't it default to text?
fopen defaults to read only and binary in Matlab, in the PC version at least.

 Similar Threads Thread Thread Starter Forum Replies Last Post ewmayer Programming 26 2011-07-08 18:22 drizzts Miscellaneous Math 1 2007-10-13 14:25 dave_0273 Programming 1 2004-04-29 02:40 tom11784 Programming 2 2003-11-29 14:30

All times are UTC. The time now is 20:03.

Fri Jul 1 20:03:37 UTC 2022 up 78 days, 18:04, 0 users, load averages: 2.01, 2.10, 2.08