Looping through several files with the same name
Mostra commenti meno recenti
Hello,
I have the following code to load a number of files with the same name
for i = 1:20
fileName = ['flow' num2str(i)];
load(fileName);
end
All files are the same size (100 * 48). For each file I would like to do the following (i've used {filename} to indicate where the file is being used)
{filename} = rad2deg({filename}); % convert all data to degrees
data = zeros(100, 48); % create an empty array to store data
for g = 1:100
for t = 1:48
data(g, t) = ({filename}(g, t)-nanmean({filename}...(g,:)))/nanstd({filename}(g, :)); % convert all data to Z score
end;
end;
I've tried to use
file = (flow(num2str(i)));
but, for some reason this returns a weird array of numbers
I'm pretty new to MatLab so any help will be greatly appreciated!
Many thanks
10 Commenti
Matt J
il 29 Ott 2012
When you do load(fileName), what is the name of the variable that you are loading? What is the name of the thing contained inside the file?
Tracey
il 29 Ott 2012
I didn't ask what the names of the files were. I asked for the names of the matrices that the files contain. For example, the following saves an array called 'A' to the file flow1.mat and then displays the contents of that file.
>> A=rand(5); save flow1 A;
>> whos -file flow1
Name Size Bytes Class Attributes
A 5x5 200 double
In this example, if someone asked you for the name of the variable inside flow1.mat, your answer would be 'A'. Or, do you mean that your files are just ASCII text files?
Tracey
il 29 Ott 2012
Modificato: Walter Roberson
il 29 Ott 2012
Matt J
il 29 Ott 2012
Please type the following commands and tell me what the output is
>> S1=load('flow1.mat');
>> whos S1,
>> display(S1),
Tracey
il 29 Ott 2012
OK. Well just so you know, the steps you have just done teach us 2 things
- The file flow1.mat does NOT contain pure numerical data. It contains a MATLAB matrix variable called 'flow1'. Presumably flow2.mat contains a variable called flow2 and so forth.
- The matrices inside your .mat files are not 100x48. They are 48x100
Tracey
il 29 Ott 2012
MATLAB variables are the named arrays and other quantities that you create in the MATLAB workspace, e.g.
>> a=1; b=[1,2 3]; s='cats and dogs'; %some variables
A .mat file is a file to which you can save collections of variables. It is very different from an Excel spreadsheet, since it may contain different variables of different shapes, sizes and types. The file itself can't be considered a rectangular array of "cells" at all.
The following is a good exercise to get a clearer picture of how .mat files work
>> save myfile.mat a b s
>> whos -file myfile.mat %Look inside the file
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x3 24 double
s 1x13 26 char
As you can see, the above creates a file called myfile.mat which holds not one, but three variables of very different types that were previously created. Furthermore, the variables contained in this file all have their own names, completely independent of the name of the file itself.
When you tell people that you want to load a .mat file, people have to know which of the possibly many variables inside the file you want to load and what their names are. The following will load back in just the variables 'a' and 's' for example
>>load myfile a s
Risposta accettata
Più risposte (1)
George
il 29 Ott 2012
0 voti
Tracey,
'flow' is a built-in matlab function for visualizing 3-D data. See >>help flow. Therefore you should not use "file = (flow(num2str(i)));"
Similar to what Matt mentioned, if you load a mat file, using e.g. load('flow15'), then to process it as your code tries, then the array found within each file must have the same name as your filename.
From your posting, I do not really understand what is failing. What, specifically is the problem?
Also, unless you have written a function 'rad2deg', I do not think that Matlab has a built-in rad2deg.
4 Commenti
Tracey
il 29 Ott 2012
George
il 29 Ott 2012
After you load one of the files, e.g. 'file1', into an empty workspace, what variable name(s) do you see in the workspace, and what are the variable's dimensions? [100 x 48]? Alternatively, type 'whos' in the command window to get var name and size.
Tracey
il 29 Ott 2012
Tracey
il 29 Ott 2012
Categorie
Scopri di più su Structures in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!