MATLAB won't open a file created by Octave - matlab

I generated and saved large number of data files using Octave, and now I need to open them in MATLAB as part of analyzing them. MATLAB spits out this error.
Error using load
Unable to read MAT-file
[file path]/PhPar_40.mat: not a binary MAT-file.
Try LOAD -ASCII to read as text.
Trying its suggestion of load -ASCII then gives this error.
Error using load
Number of columns on line 2 of ASCII file
[filepath]/PhPar_40.mat must be the same as previous lines.
I (now) understand that Octave is capable of saving in a MATLAB readable format, but re-creating these data files would take an inordinate amount of time and really isn't an option. Is there a way to get MATLAB to read these files?

MATLAB can't open these files because these are not saved by octave properly. Try saving them in octave by following command:
save -mat7-binary '[filepath]/PhPar_40.mat' 'm'
If you have large number of files, you can place all files in folder and then run an iterator to read all load and save in correct format automatically. This iterator will look like:
file = dir('[filepath_read]/*.mat');
index = 1;
while (index==length(file)+1)
m = load('file(index).name')
save -mat7-binary strcat("[filepath_write]/", file(index).name, ".mat") 'm';
index = index+1;
pause(1);
endwhile
Once you have all the files converted to right format, load them in MATLAB. I hope it will solve your problem

Related

save specific files with MATLAB commands

I'm trying to save models in oldest MATLAB versions as below
I look for each folder and subfolder to find any .mdl or .slx to save it as 2007b version
The problem I have is :
it works if I just look for one extension whereas I'm wondering
to do that on each .mdl and.slx .
the save_system takes too much
time
Do you know how could I get all .mdl and .slx and is there an optimized way to save ?
Thanks
rootPath = fullfile('M:\script\ytop','tables');
files = dir(rootPath );
for ii = 3:numel(files)
x = fullfile(rootPath ,files(ii).name);
cd(x);
mdl = { dir('*.mdl'),dir('*.slx')}; % here it works if only I set dir('*.mdl')
for jj = 1:numel(mdl)
load_system(mdl(jj).name);
save_system(mdl(jj).name,mdl(jj).name, 'SaveAsVersion','R2007b');
end
end
%here you used {} which created a cell array of two structs. cat creates a single struct which.
mdl=cat(1,dir('*.mdl'),dir('*.slx'));
for jj = 1:numel(mdl)
[~,sysname,~]=fileparts(mdl(jj).name);
load_system(mdl(jj).name);
%use only sysname without extension. R2007b is mdl only. You can't store files for R2007b in slx format
save_system(sysname,sysname, 'SaveAsVersion','R2007b');
%close system to free memory.
close_system(sysname);
end
Applying only the required fixes your code has one odd behaviour. For mdls the file is replaced with the original one, for slx a mdl is created next to the original one. You may want to add a delete(mdl(jj).name) after loading.

Loading huge binary file partially into Matlab

I have a huge binary file with double precision numbers and I would like to load parts of it into Matlab. Is there a way to do this?
One way would be if I could convert it to a .mat file (without loading it in Matlab first), but I haven't been able to figure out how (or if it's actually possible).
Any ideas?
PS: I was thinking of using c++ to do the conversion but it turns out this is really problematic because I'm using a linux version of c++ (through cygwin) and a windows version of Matlab.
If you know what parts of the file you want to load, you can use fseek followed by fread (both preceded by fopen, of course).
For example, jump a few thousand bytes into a file and read a certain number of bytes, as doubles:
fid = fopen('binary.dat','r');
fseek(fid, 3000, 'bof');
A = fread(fid, N, 'double');
fclose(A); % don't forget to close the file
See the section of documentation called Reading Portions of a File for more information.

Importing binary LabVIEW files with header information into MATLAB?

I have large .bin files (10GB 60GB) that I want to import to MATLAB; each binary file represents the output of two sensors, thus there are too columns of data. Here is a more manageable sized example of my data.
You will notice that there is a .txt version of the data; I need to upload the .bin files directly to MATLAB, I can't use the .txt version because it takes hours to convert with larger files.
The problem I have is that the .bin file has header information that I can't seem to interpret properly, and thus I cannot extract the data in MATLAB every time I try I seem to get gibberish values.
This is all the information I have about the binary header:
Loading Labview Binary Data into Matlab
LabVIEW Data Logger: Binary Header File Format
Any help/advice would be much appreciated I have been trying to solve this problem for days now.
P.S. Someone has already written a function to solve this problem but it does not seem to work with my binary data (could be something to do with the dimensions/size of my data): http://www.mathworks.co.uk/matlabcentral/fileexchange/27195-load-labview-binary-data
Below is the code that I am using to import my data, I believe that that d1 and d2 are the dimensions of my binary data. D2 is probably incorrect for the example file in the dropbox because it has been truncated.
The problem I have is that the code extracts my data and I know it is correct because I can check it with the .txt file (also in the drop box) however there are seaming random bad values between the good data points. These bad values result from the following strings following strings: "NI_ChannelName", "Sensor A", "Sensor B", "NI_UnitDescription", and "Volts" scatted throughout the binary file.
clear all
clc
fname = 'RTL5_57.bin';
fid = fopen(fname,'r','ieee-be');
d1 = fread(fid,4);
trash=fread(fid,2,'double');
d2 = fread(fid,4);
trash=fread(fid,1,'double');
data=fread(fid,'double');
I suppose you will need to change the data-format. See Matlab help.
https://decibel.ni.com/content/docs/DOC-39038
Scope:
1) Write a binary file in matlab and read into labview. 2) Write a binary file in labview and read into matlab.
Background:
IMPORTANT:
You must know (3) things about the binary data in the file before you can read the data:
1) what binary format (precision) was used to store the data
2) the exact number of values in the file to read.
3) Endianness
There is no row or column in binary files. Think of a long row/or a long column that needs to be mapped to a 2D array.
Resources on data in binary format.
http://cse.unl.edu/~sincovec/Matlab/Lesson%2024/Binary/CS211%20Lesson%2024%20-%20Binary%20File%20Input-Output.htm

Saving data to file in different path in Matlab

I am trying to save some data from current workspace in Matlab to a different folder. I tried using
save('c:\stp\vtp\train.txt','data','-ASCII');
where data is a double matrix. It gives me error message
??? Error using ==> save
Unable to write file c:\stp\vtp\train.txt: No such file
or directory.
I tried using fullfile syntax, even then it is the same case. My current working folder is in different path.
You probably need to run mkdir first. For example:
%Some data to save
x = 1;
%Try to save it in a deep, non-existent directory
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x');
% This will probably recreate your error
%To fix, first create the directory
mkdir(fullfile(tempdir,'sub1','sub2','sub3','sub4'))
%Now save works
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x') %No error

loading multiple .mat files in MATLAB

I have 110 files named time1.mat, time2.mat ..., time110.mat. I want to load these matrices into the MATLAB workspace.
I have always used load -'ASCII' matrix.mat to load an ASCII matrix file in the current folder.
So I tried doing
for i=1:10
filename=strcat('time',int2str(i),'.mat');
load -'ASCII' filename
end
But I am getting a MATLAB error as
??? Error using ==> load
Unable to read file filename: No such file or directory.
�
Of course the string filename seems to be evaluated correctly by MATLAB as time1.mat. in the first iteration where it crashes at the load line.
Any suggestions how I should do this?
Use load(filename, '-ascii')