Actually I am storing single precission values in a variable and want to read that variable in uint8 precission and store it in another file for the purpose I write scripts as fwrite(file1,readtext(inertial_data,'uint8'),'uint8'); it displays an error stating----Undefined function 'readtext' for input arguments of type 'single' and another script as fwrite(file1,fread(inertial_data,'uint8'),'uint8'); it displays error stating------Invalid file identifier. Use fopen to generate a valid file identifier.
what will be the correct identifier to read a single precission variable.

 Réponse acceptée

Thorsten
Thorsten le 24 Nov 2015
Modifié(e) : Thorsten le 24 Nov 2015
For writing and reading to files in Matlab you need a so called file identifier, or fid, that is generated using fopen and the name of the file.
Suppose that you have your single precision data in variable A, that you want to write to file test.bin as uint8, you can use:
fid = fopen('test.bin', 'wb'); % create the fid
assert(fid ~= -1, 'Cannot open file.') % check that is is valid
fwrite(fid, uint8(A), 'uint8') % write data

3 commentaires

Nikhil Singh
Nikhil Singh le 25 Nov 2015
I have tried the solution given by you somehow it working but in command window a message is also popping i.e. Caught "std::exception" Exception message is: Message Catalog MATLAB:legacy_two_part was not loaded from the file. Please check file location, format or contents
Nikhil Singh
Nikhil Singh le 25 Nov 2015
I also added a counter to know whether it is reading all the values or not but when I am accessing that variable it shows a similar (not exactly same) message as Caught "std::exception" Exception message is: Message Catalog MATLAB:services was not loaded from the file. Please check file location, format or contents
Nikhil Singh
Nikhil Singh le 25 Nov 2015
Mr. Thorsten I am using the following piece of code filename = 'imu_data.bin'; [inertial_data,time_stamps,raw_data]=parse_imu_data(filename); %delete(filename); %A = inertial_data; i=0; while i >=0 fid = fopen('test22.bin', 'wb'); % create the fid assert(fid ~= -1, 'Cannot open file.') % check that is is valid fwrite(fid, uint8(inertial_data), 'uint8'); % write data i = i+1; fclose(fid); end %fclose(fid); % Plot data in SI units figure(1),clf, hold on plot(inertial_data(1:3,:)'); plot(sqrt(sum(inertial_data(1:3,:).^2)),'c'); grid on title('Accelerometer readings'); xlabel('sample number') ylabel('a [m/s^2]'); figure(2), clf, hold on plot(inertial_data(4:6,:)'*180/pi); plot(sqrt(sum((inertial_data(4:6,:)*180/pi).^2)),'c'); grid on title('Gyroscope readings'); xlabel('sample number') ylabel('\omega [deg/s]');
and the count of inertial_data is 402.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 24 Nov 2015
Why do you think you should have a function called readtext()? I don't. What happens when you do this:
>> which readtext
'readtext' not found.
Also for the other error, the file identifier, which you call file1, it says is unidentified. Did you ever have code like this to define file1?
fullFileName = 'C:/whatever/file.dat';
file1 = fopen(fullFileName);
if file1 == -1
% File not found
errorMessage = sprintf('Error: this file does not exist:\n%s', fullFileName);
uiwait(errordlg(errorMessage));
return;
end

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by