mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2004-04-27, 15:54   #1
dave_0273
 
dave_0273's Avatar
 
Oct 2003
Australia, Brisbane

2×5×47 Posts
Default 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??
dave_0273 is offline   Reply With Quote
Old 2004-04-27, 16:47   #2
geoff
 
geoff's Avatar
 
Mar 2003
New Zealand

100100001012 Posts
Default

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')
geoff is offline   Reply With Quote
Old 2004-04-27, 17:53   #3
geoff
 
geoff's Avatar
 
Mar 2003
New Zealand

48516 Posts
Default

Sorry I was thinking C, the sscanf line should just be

exponent = scanf(s, '%d')
geoff is offline   Reply With Quote
Old 2004-04-27, 19:49   #4
Uncwilly
6809 > 6502
 
Uncwilly's Avatar
 
"""""""""""""""""""
Aug 2003
101×103 Posts

297316 Posts
Default

Check out this thread. Maybe you can talk nice to one of the folk there to make any changes that would suit you....
Uncwilly is offline   Reply With Quote
Old 2004-04-27, 20:04   #5
aaronl
 
aaronl's Avatar
 
Aug 2003

24·3 Posts
Default

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.)
aaronl is offline   Reply With Quote
Old 2004-04-28, 03:39   #6
dave_0273
 
dave_0273's Avatar
 
Oct 2003
Australia, Brisbane

1110101102 Posts
Default

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)???
dave_0273 is offline   Reply With Quote
Old 2004-04-28, 06:33   #7
Uncwilly
6809 > 6502
 
Uncwilly's Avatar
 
"""""""""""""""""""
Aug 2003
101×103 Posts

245638 Posts
Default

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'
Uncwilly is offline   Reply With Quote
Old 2004-04-28, 06:39   #8
dave_0273
 
dave_0273's Avatar
 
Oct 2003
Australia, Brisbane

1110101102 Posts
Default

Thanks uncwilly - that worked perfectly!
dave_0273 is offline   Reply With Quote
Old 2004-04-28, 14:24   #9
geoff
 
geoff's Avatar
 
Mar 2003
New Zealand

13·89 Posts
Default

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.
geoff is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matlab to C/C++ converter ewmayer Programming 26 2011-07-08 18:22
Matlab fzero help drizzts Miscellaneous Math 1 2007-10-13 14:25
More Matlab Help Please... dave_0273 Programming 1 2004-04-29 02:40
small matlab program for results file 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

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.

≠ ± ∓ ÷ × · − √ ‰ ⊗ ⊕ ⊖ ⊘ ⊙ ≤ ≥ ≦ ≧ ≨ ≩ ≺ ≻ ≼ ≽ ⊏ ⊐ ⊑ ⊒ ² ³ °
∠ ∟ ° ≅ ~ ‖ ⟂ ⫛
≡ ≜ ≈ ∝ ∞ ≪ ≫ ⌊⌋ ⌈⌉ ∘ ∏ ∐ ∑ ∧ ∨ ∩ ∪ ⨀ ⊕ ⊗ 𝖕 𝖖 𝖗 ⊲ ⊳
∅ ∖ ∁ ↦ ↣ ∩ ∪ ⊆ ⊂ ⊄ ⊊ ⊇ ⊃ ⊅ ⊋ ⊖ ∈ ∉ ∋ ∌ ℕ ℤ ℚ ℝ ℂ ℵ ℶ ℷ ℸ 𝓟
¬ ∨ ∧ ⊕ → ← ⇒ ⇐ ⇔ ∀ ∃ ∄ ∴ ∵ ⊤ ⊥ ⊢ ⊨ ⫤ ⊣ … ⋯ ⋮ ⋰ ⋱
∫ ∬ ∭ ∮ ∯ ∰ ∇ ∆ δ ∂ ℱ ℒ ℓ
𝛢𝛼 𝛣𝛽 𝛤𝛾 𝛥𝛿 𝛦𝜀𝜖 𝛧𝜁 𝛨𝜂 𝛩𝜃𝜗 𝛪𝜄 𝛫𝜅 𝛬𝜆 𝛭𝜇 𝛮𝜈 𝛯𝜉 𝛰𝜊 𝛱𝜋 𝛲𝜌 𝛴𝜎𝜍 𝛵𝜏 𝛶𝜐 𝛷𝜙𝜑 𝛸𝜒 𝛹𝜓 𝛺𝜔