PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
%% PICOSCOPE mat files are corrupted
% Copyright Ivan Izotov, IAP RAS, 2016
clearvars;
ddin='.';
ddout='FIXED mat';
if ~exist(fullfile(ddin,ddout),'dir')
mkdir(ddin,ddout);
end
fls=dir(fullfile(ddin,'*.mat'));
fixed_count=0;
for j=1:numel(fls)
fn=fullfile(ddin,fls(j).name);
F=fopen(fn);
[pathstr,name,ext] = fileparts(fn);
Fout=fullfile(ddin,ddout,fls(j).name);
file_ok=1;
i=1;
V='''ORIGINAL_FILE''';
ORIGINAL_FILE=fn;
while ~feof(F)
h5=fread(F,5,'int32');
%{
h5(1) - type; 0->double, 10->single, 20->integer
h5(2) - length;
h5(3) == 1;
h5(4) == 0;
h5(5) == length of name including \0 character;
%}
if length(h5) < 5
fprintf('Header corrupted at variable #%d.\n', i);
file_ok=0;
break;
end
if h5(2) == 0
fprintf('Found bad 0-byte size at variable #%d.\n', i);
file_ok=0;
break;
end
data=fread(F,h5(5),'*char')';
var_name=data(1:end-1);
switch h5(1)
case 0
data_type='double';
case 10
data_type='single';
case 20
data_type='int';
otherwise
fprintf('Unknown var type at variable #%d.\n', i);
file_ok=0;
break
end
data=fread(F,h5(2),data_type);
eval([var_name '=data;']);
% %% fixing header
% h5(2)=length(data);
V=[V ',''' var_name ''''];
i=i+1;
end
if feof(F)
fprintf('%s file FIXED.\n',fls(j).name);
fixed_count=fixed_count+1;
%% writing fixed file
eval(['save(Fout,' V ',''-mat'');']);
eval(['clearvars(' V ');']);
else
fprintf('%s file ERROR',fls(j).name);
end
fclose(F);
end
fprintf('%d files out of %d were fixed.',fixed_count,numel(fls));
clearvars;