Pull the same variable from multiple .mat files and put into an open office calculator or .txt

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!

Antworten (2)

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 Kommentare

This it what I created but it didnt seem to work. I decided to go with just saving it as a .txt file becasue I can copy and paste it over into an openoffice file easily
dir('Cake_E*.mat');
n=1
for dir > 1
load(dir);
value = A.List.Array;
%xlswrite
save('Cake' & n & '.txt','A.List.Array', '-ASCII', '-append');
n=n+1
end
Any suggestions? Thanks
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.

Melden Sie sich an, um zu kommentieren.

Guillaume
Guillaume am 17 Sep. 2014
Bearbeitet: Guillaume am 17 Sep. 2014
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, ...);

Kategorien

Mehr zu Environment and Settings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 16 Sep. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by