Loading a mat file consisting a variable

1 visualizzazione (ultimi 30 giorni)
Marium Malik
Marium Malik il 2 Dic 2012
I have a function in which the value of variable b comes from another function. I have mat files saved named des1,des2,des3,des4..... 1,2,3,4... correspond the values of b. I want des(b).mat to be loaded. what could be the method of doing it?

Risposte (2)

Image Analyst
Image Analyst il 2 Dic 2012
Please see the FAQ for some code examples: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F The first example does what you want, though I left out some robustness for the sake of simplicity. To be robust, you'd use exist() to check that the file actually exists before reading it. Or you could just check to make sure that you have a list of only what you know exists. Thus you'd adapt the FAQ like this:
myFolder = 'C:\Documents and Settings\yourUserName\My Documents';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, 'des*.mat');
matFiles = dir(filePattern);
for k = 1 : length(matFiles)
baseFileName = matFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
storedStructure = load(fullFileName);
b = storedStructure.b;
% Now do something with b - whatever you want.
end
This is a fairly robust way of processing your files, and it could eliminate some errors, like you're looping over des1 through des500, but for some weird reason, des348.mat and des412.mat don't exist, which would generate an error if you tried to create the filenames systematically with sprintf() inside the loop and didn't check first with exist() before calling load().

Azzi Abdelmalek
Azzi Abdelmalek il 2 Dic 2012
Modificato: Azzi Abdelmalek il 2 Dic 2012
for k=1:4
data=load(sprintf('des%d',k))
end
data is a struct variable, if file des1 contains one variable b, to get b:
b=data.b

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by