Creat a loop to generate a a mean value of data which I got form TXT files then plot the results

1 回表示 (過去 30 日間)
I trying to do script to evalute my lab data into matlab, I have lots of txt files for many samples, each sample has 30 txt files. I did a function to get the data from these files and store them into a structure that contains data and labels. I would like to know if it is possible to load all the 30 files using loop instead of line by line .
function s = try1(fn) % adapt for other file format according to your needs fid = fopen(fn); s.data = []; % skip first lines
for k=1:6
l = fgetl(fid);
% read header
a = regexp(l,',','split');
s.labels = a;
l = fgetl(fid);
k=0;
end
while( (l~=-1)&(k<130) )
l = l(1:end-1);
a = regexp(l,', ','split');
a = regexpre
p(a, ',','.');
s.data = [s.data; str2double(a(2:4))];
l = fgetl(fid);
k = k+1;
end
fclose(fid);
end
then i used this code to fetch the data form folder directory
dataDirectory = 'c:/myDirectory';
allFilesDir = dir(fullfile(dataDirectory , '*.txt'));
allFN = {allFilesDir.name}
% Or do this:
% allFN = {'file1.txt', 'file2.txt'};
result = [];
for ind = 1:length(allFN)
myFN = fullfile(dataDirectory, allFN{ind});
result(ind) = try1(myFN);
end
% Now get the mean:
myMean = mean([result.data])
but i got this error The following error occurred converting from struct to double: Error using ==> double Conversion to double from struct is not possible. Error in ==> getmean at 13 result(ind) = try1(myFN); any idea about porb \\

採用された回答

ahmed
ahmed 2013 年 9 月 23 日
I went to 3D array option then squeeze my results to get the results in this way
result(:,:,n)
G=(squeeze(myMean));
But I got them into 3*35 not 35*3 I will try to find a way to flip the data as I want ..

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 9 月 20 日
It's having trouble taking the mean of the entire structure array. Each member (.data) could be a different sized array as far as it knows, even though they may not be, so it just throws up its hands. There is nothing to say that s(1).data and s(2).data have the same number of rows or columns so it's saying it doesn't know how to concatenate then into one big long column vector. You may need to get the mean of each structure member one at a time inside the loop
myMean(ind) = mean([result(ind).data])

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by