필터 지우기
필터 지우기

To extract the random integer into a file

조회 수: 1 (최근 30일)
fiteri razali
fiteri razali 2011년 12월 27일
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.
fiteri razali
fiteri razali 2011년 12월 27일
10Q

댓글을 달려면 로그인하십시오.


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