how to get back numerical value

조회 수: 1 (최근 30일)
Elysi Cochin
Elysi Cochin 2019년 3월 21일
편집: Elysi Cochin 2019년 3월 22일
V = [-0.00153846153846154; 0; -0.000769230769230769; -0.0126923076923077; 0.00384615384615385];
M = dec2bin(typecast(double(V),'uint8'));
Mbin = reshape(dec2bin(double(M),8).',[],1);
Mbint = Mbin';
fid = fopen('temp.txt','w');
fprintf(fid, '%s', Mbint);
fclose(fid);
fid = fopen('temp.txt','r');
F = fread(fid);
is there any method, where i can get vector V back from F

채택된 답변

dpb
dpb 2019년 3월 21일
편집: dpb 2019년 3월 21일
F1_char = num2str(F_double);
fprintf(fid, '%s', F1_char);
You wrote the string representation of the values of F_double sequentially into the file in memory storage order which is column major. Hence, in the file, the values are actually ordered by column, not row. Example of what such a file looks like:
>> X=[pi;2.4];
>> num2str(X)
ans =
2×6 char array
'3.1416'
' 2.4'
>> fprintf('%s',num2str(X))
3 . 1 421.64
>>
You see that the value of pi is NOT written sequentially to the file but the file contains the first character of each of the two character array elements in order, then the second two characters, etc., etc., ... In other words, you've scrambled-up the order in the file and sequentially when read back the first six characters are '3b.b1b' where 'b' represents the blank.
To avoid that, you would have to transpose the array F1_char before writing it.
To write the double vector to a text file use
fid = fopen('temp.txt','w');
fprintf(fid, '%f\n', F1_double);
fclose(fid);
fid = fopen('temp.txt','r');
F2_double = fscanf(fid,'%f');
fclose(fid);
For input of simple text files,
F2=importdata('temp.txt');
is less complex.
For just saving and retrieving a variable for temporary purposes in Matlab, see
doc save
doc load

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by