how to get back numerical value

1 Ansicht (letzte 30 Tage)
Elysi Cochin
Elysi Cochin am 21 Mär. 2019
Bearbeitet: Elysi Cochin am 22 Mär. 2019
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

Akzeptierte Antwort

dpb
dpb am 21 Mär. 2019
Bearbeitet: dpb am 21 Mär. 2019
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

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by