To extract the random integer into a file

6 views (last 30 days)
HEllo guys,
am new to this matlab, my Q is probably very simple for you guys.please help me. I want to extract the random integer from the script
data = randint(1,48,2) to a new file so that the same data can be called for and use for the next iteration. someone can help me please.

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 27 Dec 2011
If you get a little confused with fread and fwrite, then
you can try another command fprintf n fscanf
Here I give you sample code :
Let say you want to store 'data = randint(1,48,2)' into txt file.
data = randint(1,48,2)
fid = fopen('data01.txt', 'w');
fprintf(fid,'%d ',data);
fclose(fid);
And for recalling this data later, you can read from the text file you have created
fid = fopen('data01.txt');
m5 = fscanf(fid,'%d')'
fclose(fid);
Ask me again if it still difficult to understand.
  3 Comments
fiteri razali
fiteri razali on 27 Dec 2011
ok Chandra. lets me interpret your coding according to my understanding after reading the HELP.please comment accordingly if my interpretation is wrong.
let say the first code as below
=========================================
data = randint(1,48,2)
fid = fopen('data01.txt', 'w');
fprintf(fid,'%d ',data);
fclose(fid);
==========================================
1)data = randint(1,48,2)
the "data" variable containing the generated random scalar that is either 0 or 1, with equal probability which was generated by randint command
2)fid = fopen('data01.txt', 'w');
fopen will open a filename 'data01.txt' . 'w' is a permission specifiers that specifies the opened file, or the new file created is for writing and as well discard existing contents, if any.
3)fprintf(fid,'%d ',data);
fprintf will format the data in the real part of matrix data under format string %d(decimal sign)and will write it to the file associated with file identifier fid which is 'data01.txt'
4)fclose(fid)
fclose will close the specified file which was opened. and in this code the file to be closed is 'data01.txt'
fiteri razali
fiteri razali on 27 Dec 2011
for
==================================
fid = fopen('data01.txt');
m5 = fscanf(fid,'%d')'
fclose(fid);
==========================================
1)fid = fopen('data01.txt')
will open the 'data01.txt' file
2)m5 = fscanf(fid,'%d')'
will read data from the file specified by fid which is 'data01.txt', converts it according to the specified format string which is %d, and returns it in matrix m5.
3)fclose(fid)
fclose will close the specified file which was opened. and in this code the file to be closed is 'data01.txt'

Sign in to comment.

More Answers (3)

Chandra Kurniawan
Chandra Kurniawan on 27 Dec 2011
You can use fwrite command to write array into binary file.
And you can use fread to read data from binary file
  2 Comments
fiteri razali
fiteri razali on 27 Dec 2011
thanks a lot, I will try and get back if i failed. in the process of learning matlab :-), so even simple thing can be complicated for me..
fiteri razali
fiteri razali on 27 Dec 2011
Chandra, I have read about fwrite and fread in the HELP but quit difficult to understand.actually i want to extract the random integer data in the rustam STTC file to a new file
in the rustam STTC.m under Data the code
data = randint(1,48,2) i want to extract the data and put into another file.so that i can recalled the data back later on without generating new random integer. i try writing for start
clear; clc; fid = fopen('STTC.m');
then got blank. arrhghh...i need it urgently
hope u can help me Chandra

Sign in to comment.


fiteri razali
fiteri razali on 27 Dec 2011
Chandra, I read the HELP on fprintf. under syntax there is
count = fprintf(fid, format, A, ...)
what actually count? i really confuse because in the example u gave there is no count.
fprintf(fid,'%d ',data);
same goes to fclose in the HELP file. under syntax given
status = fclose(fid)
what actually status is? do we need to include status and count in our programming.sorry for asking that kind of questions.waiting for your reply
  2 Comments
Walter Roberson
Walter Roberson on 27 Dec 2011
"count = fprintf(...) returns the number of bytes that fprintf writes."
"status = fclose(...) returns a status of 0 when the close operation is successful. Otherwise, it returns -1."
There are situations where knowing the count or status is important, but your situation is not one of them.

Sign in to comment.


Friedrich
Friedrich on 27 Dec 2011
Hi,
when you want to use the data back in MATLAB again, the best would be a mat file, since it pretty easy:
data = randint(1,48,2)
save('filename.mat','data')
load('filename.mat')
  2 Comments
fiteri razali
fiteri razali on 27 Dec 2011
friedrich
the filename can be anything?
if i put it
data =randint (1,48,2);
save('data01.mat','data');
will it be right?
Friedrich
Friedrich on 28 Dec 2011
yes, filename can be whatever you like.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!