automating script using uiload

9 views (last 30 days)
jakob ekwall
jakob ekwall on 20 Jan 2016
Edited: Kirby Fears on 20 Jan 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
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?
jakob ekwall
jakob ekwall on 20 Jan 2016
Edited: jakob ekwall on 20 Jan 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)

Sign in to comment.

Accepted Answer

Kirby Fears
Kirby Fears on 20 Jan 2016
Edited: Kirby Fears on 20 Jan 2016
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']);
Kirby Fears
Kirby Fears on 20 Jan 2016
Edited: Kirby Fears on 20 Jan 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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 20 Jan 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.


Find more on Descriptive Statistics in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by