Matlab read and write to text file

Status
Not open for further replies.

ahmad_abdulghany

Advanced Member level 4
Joined
Apr 12, 2005
Messages
1,206
Helped
102
Reputation
206
Reaction score
22
Trophy points
1,318
Location
San Jose, California, USA
Activity points
11,769
matlab read text file

Assalamo alaykom
i want anyone help me to read and write to a text file with matlab ..

what i know about this .. ( i don't know anyother thing .. i need to know every other things about this )

to write to text file :
****************
fid=fopen('file_name.txt','w' ); % i want to know other operands like 'w'
fprintf(fid,'text to be written formatted'); % i want to know if there are other options to do
fclose=(fid);

to read from text file :
******************
Sorry .. i don't know how to do this .. and know how to do ..
as an application to learn me that ..

assmume that i have a text file that contains binary data in separate lines ..each line contains one word of 42 bits ... this word is written after a lable in its line, and i have 10 lines ..

like that ..
__________________________________________________________________
label1='010001010100101001010010010011111101011010'
label2='110110101011111101000001111110101010010111'

............
__________________________________________________________________
and so on ..
now .. how can i read data from such file .. and store the bits beside each label in separate variables .. each variable should be a vector containing all bits of line as elements .. i.e. each variable is a 42 element vector..

like that : var1 =[0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 0]

i hope you can understand me ... also can help me ..
 

matlab read file

to read from a file you can use "fread" type "help fread" to learn more. There are also different functions for this purpose.

you can use this code:

fid = fopen('???.txt', 'r');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
{here write the code to pick up the binary numbers in tline and put them in different variables Note that you have char s in tline}

fclose(fid);

Also look for "eval" in help you may need it for different variables
 
matlab read line

first . thank you for your reply ..
second ..what is the operator ~ ?? and what is the function ischar (tlin) ?
please give some more details ..

thanks in advanse
 

read file matlab

help ischar
help ~

If you are really stuck: help help

doc works too.

MATLAB has excellent online help.
 

matlab write text file

echo47 said:
help ischar
help ~

If you are really stuck: help help

doc works too.

MATLAB has excellent online help.

i typed the syntax you said .. and i understood what does the ischar mean .. but i didn't understand what is the ~ .. sorry .. if you can expalin it .. please do ..
thenk you ..
 

read text file matlab

The ~ is a unary operator that performs "logical NOT". MATLAB provides two syntaxes: ~x and not(x). They give you 1 if x is zero, or 0 if x is non-zero.

Hope that helps!
 

matlab append to file


I am sorry to ask in this topic again .. but i dont know why i cant understand the above code !

I am sorry again ..but please rewrite it with comments or explain its parts ..

thank you very much for help
 

matlab read from file

Dear ahmad_abdulghany,
Walakum Salam,
I think your problem is not a bigger one,
You type
help fopen then matlab will tell you all that you need
e.g.

FID = FOPEN(FILENAME,PERMISSION) opens the file FILENAME in the
mode specified by PERMISSION. PERMISSION can be:

'r' read
'w' write (create if necessary)
'a' append (create if necessary)
'r+' read and write (do not create)
'w+' truncate or create for read and write
'a+' read and append (create if necessary)
'W' write without automatic flushing
'A' append without automatic flushing

similarly type
help fread
help fwrite

Matlab will also print one example.
Atleast it's the case on my pc i have Matlab 6.5
 
matlab write line to file

fid = fopen('???.txt', 'r');
// you opened your text file. fid is a file id (a pointer) to the file that you will use through your program

while 1
// Enters an infinite loop. It will stop when you say break


tline = fgetl(fid);
//get one line from the text file and put it in the 'tline' . Notice that in matlab 'tline' is an array (of characters this time because fgetl says so)

if ~ischar(tline), break, end
//Here is where you say stop or go. In each loop of while you get a line from the text file and then fid will show the next line in the file. (for example in first fgetl(fid) you get the first line and fid goes to the second line i.e. pointing at the second line. This process continues until you read a line which is actually a line meaning the end of the text file i.e. at the end of the file fgetl will get an empty line nothing in it as a char and ischar(tline) will return logical zero and ~ischar(tline) will then mean not zero=logical true so you will enter if statement and break command will execute

{here write the code to pick up the binary numbers in tline and put them in different variables Note that you have char s in tline}
// You can reach this part if you have not reach the end of the text file. Here you have a line of the text file in 'tline'. Play with it and take the data you need from it



The code that I wrote is taken from matlab help and actually you can do in different ways. The part in {} is actually hard to write for you because it is a specific part of your work. So I recommend you to look at eval function but it is a bit advanced. You must learn to use help or look for some books about matlab.

Sorry for my language in this writting. It could not be formal. Because I try to be as fast and short as possible.

Keep on trying

Regards
 
matlab file read

emrek said:
Sorry for my language in this writting. It could not be formal. Because I try to be as fast and short as possible.

Keep on trying

Regards

Thank you very much for your very useful help .. I am really very happy to learn these many things in Matlab even i didn't even imajine that there is any similar thing in Matlab !!

I realized now that Matlab is a very powerful tool !!

Sorry for my basic quistions .. but always remember that i am a beginner and wanna learn ... also remeber how nice thing that is to help other people, so go on ... and dont' be sad from me

thank you all for help .. Ahmad
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…