automating script using uiload

4 visualizaciones (últimos 30 días)
jakob ekwall
jakob ekwall el 20 de En. de 2016
Editada: Kirby Fears el 20 de En. de 2016
I have multiple .mat (8760x4 table) files which I want to do the same analysis on, one at a time. The script I've written works well with one of these .mat files, the one I used when I wrote the script. However, now I want to be able to use the script with all my .mat
This is what the code looks like now.
load('examplefile') %imports abc table to the workspace
xsource=linspace(datenum('01/01/14 00:00','dd/mm/yy HH:MM'),datenum('31/12/14 23:00','dd/mm/yy HH:MM'),8760); %creates a vector with datnum to use as x-axis
frb=abc.Vrde; %Gets column Vrde from table abc
qual=zeros(length(frb),1); %creates a vector with zeros with the same length as frb
ts=timeseries(frb,xsource,qual); %creates a timeseries with frb,xsource,qual
ts.QualityInfo.Code= [0 1 2 3 4 5]; %different quality flags
ts.QualityInfo.Description= {'A' 'B' 'C' 'D' 'E' 'F' }; % Display name of the quality flags
When I run the script, I want Matlab to ask me which file to load. In order to do so I used uiload. This lets me chose which ever .mat file I want to perform the analysis on. This loads a table into the workspace, and it is at this point I get stuck. There is probably an easy way but I can't really see it.
Every .mat file has the same structure, So what I need to automate is creating the
frb=abc.Vrde
But with instead of abc I want to use the name of the table that was loaded using uiload. The rest of the script is based on the timeseries ts.
Is there any way to work around this?
  2 comentarios
Kirby Fears
Kirby Fears el 20 de En. de 2016
Editada: Kirby Fears el 20 de En. de 2016
Is the issue that you have a collection of .mat files that each have analogous data in the "abc" table, but the name of "abc" is different within each .mat file?
You need to know something about the name in order to make this work. Is your table "abc" the only thing in each .mat file? Otherwise, do you have a way to identify the table, such as a naming convention?
E.g. is abc always called ['results_',datestr] ?
jakob ekwall
jakob ekwall el 20 de En. de 2016
Editada: jakob ekwall el 20 de En. de 2016
Each .mat file has a name, such as abc.mat, in that .mat file there is a table with the same name. (The table is the only thing in the .mat file)
I'll add a picture to clarify.
(In this case the table in Lagzon.mat is called Lagzon)

Iniciar sesión para comentar.

Respuesta aceptada

Kirby Fears
Kirby Fears el 20 de En. de 2016
Editada: Kirby Fears el 20 de En. de 2016
jakob,
You can get the variables loaded in your workspace with the who command.
Try out this example:
m2 = magic(2);
m3 = magic(3);
myVars = who;
In case your workspace already has several variables, you can directly get the variable names inside your .mat file like this:
myVars = who('-file','mydata.mat');
Now myVars contains the variables names inside of mydata.mat.
Assuming you have a way of identifying the right variable name from within myVars, you can extract Vrde like this:
myColumn = eval([myVars{someIndex},'.Vrde']);
  3 comentarios
jakob ekwall
jakob ekwall el 20 de En. de 2016
This worked great! Thank you very much.
However it was a bit complicated for something that in my mind felt like an easy task.
I guess it would have been much easier if I had just given every table the same name when I made the .mat files instead of giving them all unique names.
Kirby Fears
Kirby Fears el 20 de En. de 2016
Editada: Kirby Fears el 20 de En. de 2016
Yeah, naming the tables the same would be a lot easier. The code could be simplified under the assumption that the table name is the same as the file name, but you might as well use this more general approach in case that changes later.
Cheers.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 20 de En. de 2016
Inside a function I would ALWAYS call LOAD with an output argument.
data = load('mymatfile.mat');
listOfVariablesFromMatfile = fieldnames(data);
firstVariable = data.(listOfVariablesFromMatfile{1});
The third line uses a dynamic field name.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by