Effacer les filtres
Effacer les filtres

Proper File Handling in Matlab

9 vues (au cours des 30 derniers jours)
Olhado
Olhado le 16 Oct 2013
Commenté : Olhado le 16 Oct 2013
Hello Community,
I have created a complex set of GUIs that i use in the process of image analysis. Once I have used the tools to identify all the objects of interest in am image I save a matlab work space that contains within it a single variable that is a cell. Once I have done this for several images I wish to apply an algorithm which processes my data in batches. To do this I use the following code
%Selects all the files I with to work on and makes sure they are on the path
[fNames,PathName,~] = uigetfile('*.mat','MultiSelect','on');
addpath(PathName)
% Create a loop to apply the process to the file
for p=1:numel(fNames)
data=importdata([PathName,fNames{p}]);
%Apply Calculations saving the result in the data variable
tempFNAME=regexp(fNames{p},'\.','split');
save([SequenceDirectory,['\',tempFNAME{1},'_Caluculation.mat']],'data', '-v7.3')
clear data
end
This Process works well for as it does update the files and then save them so that I can open them later. However, I am having an issue where the files I have opened using this method as well as other files in the directory are get corrupted periodically. As I see it this could be an issue with my Hard Drive (its a SSD drive) or an issue with poor file handling leading to corruption of files.
My questions is this. How should I be opening and closing file handles to ensure the issues with data corruption are from a faulty drive and not my code? This may be a simple question but I am very much self taught in matlab and more of a biologist than computer scientist. Thanks for the Help
Regards,
Dan

Réponse acceptée

Jan
Jan le 16 Oct 2013
I recommend to avoid adding the folder to Matlab's path. Working with absolute path names is perfect and safer. So I'd simplify your code to:
[fNames, PathName] = uigetfile('*.mat','MultiSelect','on'); % No trailing ~ in outputs
for p=1:numel(fNames)
data = importdata(fullfile(PathName, fNames{p})); % FULLFILE better than []
tempFNAME = strtok(fNames{p}, '.');
save(fullfile(SequenceDirectory, [tempFNAME,'_Caluculation.mat']], 'data', '-v7.3');
% "clear data" has no useful effect here
end
There are no openings or closings of file handles in this code, so it cannot cause any file corruptions. Either the code, which causes problems is anywhere else, or your disk is damaged. A simple run of chkdsk will reveal problems easily, if there are any.
  1 commentaire
Olhado
Olhado le 16 Oct 2013
Great Thanks for the input, That what I had suspected

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by