To extract the random integer into a file

1 次查看(过去 30 天)
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.

采纳的回答

Chandra Kurniawan
Chandra Kurniawan 2011-12-27
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 个评论
fiteri razali
fiteri razali 2011-12-27
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 2011-12-27
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'

请先登录,再进行评论。

更多回答(3 个)

Chandra Kurniawan
Chandra Kurniawan 2011-12-27
You can use fwrite command to write array into binary file.
And you can use fread to read data from binary file
  2 个评论
fiteri razali
fiteri razali 2011-12-27
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 2011-12-27
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

请先登录,再进行评论。


fiteri razali
fiteri razali 2011-12-27
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 个评论
Walter Roberson
Walter Roberson 2011-12-27
"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.

请先登录,再进行评论。


Friedrich
Friedrich 2011-12-27
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 个评论
fiteri razali
fiteri razali 2011-12-27
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 2011-12-28
yes, filename can be whatever you like.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by