MATLAB Answers

how to get back numerical value

4 ビュー (過去 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);
fid = fopen('temp.txt','r');
F = fread(fid);
is there any method, where i can get vector V back from F


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
' 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
fprintf(fid, '%f\n', F1_double);
F2_double = fscanf(fid,'%f');
For input of simple text files,
is less complex.
For just saving and retrieving a variable for temporary purposes in Matlab, see
doc save
doc load

その他の回答 (0 件)

Community Treasure Hunt


Start Hunting!

Translated by