Function to load a .mat file (which can be inside or outside matlab folder) and perform analysis for finding constant or near-zero variance values
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
I have a matlab data file that has only one variable of type 1x1 struct which has 1x680 cell which has 680 MxN double values (values can be -ve, 0 or +ve). I want to load this data file using a function and find constant and near-zero variance values in this data set.
Thanks in advance.
Accepted Answer
Walter Roberson
on 4 Feb 2018
[filename, pathname] = uigetfile('*.mat', 'Selected mat file');
if isnumeric(filename); return; end %user cancel
filename = fullfile(pathname, filename);
datastruct = load(filename);
varnames = fieldnames(datastruct);
datacell = datastruct.(varnames{1}); %there should only _be_ one, but just in case there are more
variances = cellfun(@(M) var(M(:)), datacell);
has_small_variance = abs(variances) < 1e-5;
selected_cells = datacell(has_small_variance);
7 Comments
Thanks Walter. I want to execute this as a function so that i can call it do the analysis. And the input to the function will be the mat file.
Simply wrap it in a function. You should really learn how to write functions because it's essential for you to continue in MATLAB.
function selected_cells = GetCells(pathname, filename)
if isnumeric(filename); return; end %user cancel
filename = fullfile(pathname, filename);
datastruct = load(filename);
varnames = fieldnames(datastruct);
datacell = datastruct.(varnames{1}); %there should only _be_ one, but just in case there are more
variances = cellfun(@(M) var(M(:)), datacell);
has_small_variance = abs(variances) < 1e-5;
selected_cells = datacell(has_small_variance);
Then, to call it:
selected_cells = GetCells('c:\some folder', 'whatever.mat')
You can rename GetCells to whatever you want.
So, why are you putting a double array into a cell, then putting the cell into a field of a structure? Can't you think of any other way to make it more complicated? Why not just set the field equal to the double array directly and not even use a cell? And even then, only if the structure is a structure array, otherwise just use the double array itself directly.
HT
on 6 Feb 2018
It was just a cell of double values and not struct(I mentioned struct by mistake) . But when the file is saved in datastruct, it is saved as struct. Also, this is some data that I have already and not generated by me so i am mot trying to complicate anything here. Thanks.
Can you explain what is happening in the line : variances = cellfun(@(M) var(M(:)), datacell);
var() by default works one column at a time, but you want the variance over the entire matrix within each cell, so we run a function on each cell that reshapes the content of the cell into a vector using the (:) operator, and then takes the variance of the resulting vector. That is a single numeric value for each cell entry so you do not need 'uniform', 0 in the cellfun call.
If you wanted to take the variance per column, then you would
cellfun(@var, datacell, 'uniform', 0)
but then you would need to define more clearly what it means for hte matrices to be similar in variance.
HT
on 7 Feb 2018
Thanks.
Is it possible also to calculate other values, lets say mean of the cells in this code. And for specifying the threshold (variances<some value), is it possible to give control to user for specifying the value.
tol = input('What tolerance do you want?');
means = cellfun(@mean2, datacell);
variances = cellfun(@(M) var(M(:)), datacell);
has_small_variance = abs(variances) < tol;
selected_cells = datacell(has_small_variance);
selected_variances = variances(has_small_variance);
selected_means = means(has_small_variance);
More Answers (0)
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)