From: Peter Smith on
Hi i have a number of .mat files and basically want to operate a string such that it opens a particular variable (spreadMat) from the .mat file, creates an average and then puts the row into a matrix) and then repeats this process for the rest of the .mat files

so far i have written

rootDir = 'C:\Users\Alex Eptas\Desktop\ChiXDaily\';

tmp = dir([rootDir '*.mat']);
fnames = char({tmp.name}');
clear tmp;

dates = unique(cellstr(fnames(:,1:8))) % create a list of dates of the files

for i=1: length(dates)


load('C:\Users\Alex Eptas\Desktop\ChiXDaily\dates(i), 'spreadMat')

AverageDailySpread = nanmean(spreadMat(:,1:244));

However, obviously no file names are saved as
C:\Users\Alex Eptas\Desktop\ChiXDaily\dates(i) and Matlab provides an error message.

Can any1 suggest how this can be resolved?

end;
From: ImageAnalyst on
You need to build up your filename with sprintf(), like this:
fullFileName = sprintf('C:\Users\Alex Eptas\Desktop\ChiXDaily\%s,
dates(i));
load(fullFileName , 'spreadMat')
that is, assuming dates(i) is giving you the proper string that you
want. Or else do something like this:

count = 0;
myFolder = 'C:\Users\Alex Eptas\Desktop\ChiXDaily\'; % Or whatever...
% Get list of all mat files in this folder.
matFiles = dir([myFolder '*.mat']);
% Loop through all the found files.
for Index = 1:length(matFiles)
% Get the full name of this particular mat file.
baseFileName = matFiles(Index).name;
fullFileName = fullfile(myFolder, baseFileName);
% Read in the file into a structure.
storedStructure = load(fullFileName);
% See if it has the field we're looking for.
hasField = isfield(storedStructure, 'spreadMat');
if hasField
count = count + 1;
% Compute the mean for this file
AverageDailySpread(count) = nanmean(storedStructure.spreadMat(:,
1:244));
end
end
From: Peter Smith on
Hi, thanks for such a detailed response
do you mean i need to operate like this;

clear;

rootDir = 'C:\Users\Alex Eptas\Desktop\ChiXDaily\';

tmp = dir([rootDir '*.mat']);
fnames = char({tmp.name}');
clear tmp;

dates = unique(cellstr(fnames(:,1:8)));

for i=1: length(dates);

Fullfnames = sprintf('C:\Users\Alex Eptas\Desktop\ChiXDaily\%s,dates(i)');

% when i try this i get a message saying that:
"Warning: Invalid escape sequence appears in format string. See help sprintf for valid escape sequences."
From: Walter Roberson on
Peter Smith wrote:

> Fullfnames = sprintf('C:\Users\Alex Eptas\Desktop\ChiXDaily\%s,dates(i)');
>
> % when i try this i get a message saying that:
> "Warning: Invalid escape sequence appears in format string. See help
> sprintf for valid escape sequences."

Fullfnames = [ 'C:\Users\Alex Eptas\Desktop\ChiXDaily\', dates{i} ];

Notice the use of {i} instead of (i)

If you really want to use sprintf,

FullFnames = sprintf('C:\\Users\\Alex Eptas\\Desktop\\ChiXDaily\\%s', dates{i});
From: Peter Smith on
walter that now works, but i still need to encoporate _chix.mat after the dates; i.e i need something like

> FullFnames = sprintf('C:\\Users\\Alex Eptas\\Desktop\\ChiXDaily\\%s', dates{i},_chix.mat);

any solutions??