Pull the same variable from multiple .mat files and put into an open office calculator or .txt
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm fairly new to matlab and need some help with writing a code. Heres the setup of what I want:
I have 5 files, Cake_E0001.mat, Cake_E0002.mat.mat, Cake_E0003.mat.mat, Cake_E0004.mat.mat, Cake_E0005.mat.mat. In each file, there are a set of variables; A, B, C, ect. In B there is a variable called List and in List there is an array(we will call it Array) which I need to pull out and into an OpenOffice Calc file. I know that I can call the array by typing in A.List.Array. The part im having trouble with is trying to loop through opening the files, pulling variable and exporting it to OpenOffice. Does anyone have any tips or tricks I can try or possibly even a code I can reference :)?
Thanks for your help!
0 个评论
回答(2 个)
Michael Haderlein
2014-9-16
If it's just about generating the file names, there are two options. Either you use the dir() function and get all the files which match with the pattern (dir('Cake_E*.mat')) or you generate the filenames by applying the counter (sprintf('Cake_E%04d.mat',counter)). In both cases you'll need a for loop. With load(), you can load the matfiles. I haven't worked with OpenOffice but either xlswrite() can do the job or you create some csv-file (dlmwrite()). There might be contributions in the file exchange as well, at least there are couple of results if you check for "openoffice" in the fex.
2 个评论
Michael Haderlein
2014-9-17
Apart from the issues Guillaume has described, there's some more to say:
dir('Cake_E*.mat');
You don't assign the result of dir() to any variable. You need the outcome of this function, don't you?
for dir > 1
This syntax does not exist in Matlab. Use
for n=1:length(result_of_dir_function)
instead.
load(dir);
should rather be
load(result_of_dir_function(n).name);
And there's no need to increment n by hand, this is automatically done with the for header I have shown above.
Guillaume
2014-9-17
编辑:Guillaume
2014-9-17
There are many ways to construct strings in matlab, however, & is not one them. Furthermore, you can't append numbers directly to strings (well you can but it won't give you the result you expect), you need to convert them to string first.
str = ['Cake' num2str(n) '.txt'];
%or
str = strcat('cake', num2str(n), '.txt';
%or probably clearer:
str = sprintf('cake%d.txt', n);
Furthermore, you can't directly save a subfield of a structure (you can save A.List, but not A.List.Array), so:
save(sprintf('Cake%d.txt', n), 'value', '-ascii', '-append');
Note that to save a field of a structure, the syntax would be:
save(filename, '-struct', varname, fieldname, ...);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!