MATLAB Answers

0

how to get back numerical value

Elysi Cochin さんによって質問されました 2019 年 3 月 21 日
最新アクティビティ Elysi Cochin さんによって 編集されました 2019 年 3 月 22 日
dpb
さんの 回答が採用されました
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

  0 件のコメント

サインイン to comment.

1 件の回答

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 件のコメント

サインイン to comment.



Translated by