How to post process each variable in a mat file ?

3 Ansichten (letzte 30 Tage)
Shekhar Vats
Shekhar Vats am 11 Jul. 2019
Beantwortet: Shekhar Vats am 12 Jul. 2019
I am trying to downsample all the variables in a mat file.
I can use downsample(var, newSampling) to downsample an individual variables but what to do if i need to downsample all the vectors in the mat file.
load ('my_mat_file_here.mat')
list_var = who; % to get the list of variables
for kk = 1:1:length(list_var) % to run the loop
% downsample(eval(list_var{kk}),10); <-- this part is working as expected with eval
[list_var{kk} '_1hz'] = downsample(eval(list_var{kk}),10);
end
% in this part how to define a new variable
% i am trying to use the variable name from list_var which is a character
% and add _1hs to ths string but matlab won't allow it to % be used as a varaible name
% trying to avoid using assignin & eval to assign the new variable to my base workspce
% any help appreciated.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Jul. 2019
S = load(filename)
fn = fieldnames(S)
Fc = length(fn)
for k = 1 : Fc
F = fn{k}
Data = S.(F)
Process Data
end
  1 Kommentar
Shekhar Vats
Shekhar Vats am 12 Jul. 2019
It worked for me.
Thank you Walter for a quick and helpful response. Do not know why i was shying away from using structure and accesing variables inside it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shekhar Vats
Shekhar Vats am 12 Jul. 2019
% Downsampling a given 'X' hz file to a input 'Y'hz file
%This script will load the file in a structure and append _YHz
% at the end of current variables. It will save the new file with the old
% name and append _down_sampled to it
% NOTE --> It will only DOWNSAMPLE the file. Although it can do up
% sampling, but it's not recommended to up sample data as it will just
% interpolate the data in between two points.
% You will need matlab SIGNAL PROCESSING TOOLBOX for this script to work
% In case you do not have access to the toolbox, please contact the author
% remote desktop matlab151a and matlab 151b have Signal Processing Toolbox
%% prompt dialog
tic;
prompt = {'Enter current data frequency[hz]:','Enter desired data frequency[hz]:'};
dlgtitle = 'Downsample data'; % dialog title
dims = [1 40];
definput = {'10','1'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
%% post processing of dialoge
current_freq = str2double(cell2mat((answer(1,1))));
desired_freq = str2double(cell2mat(answer(2,1)));
dwn_smpl = current_freq/desired_freq; % desired downsampling frequency
str_var = ['_' num2str(desired_freq) 'Hz']; % var name append
%% file info
[f_name, f_path]= uigetfile({'*.mat'}, 'Select a mat file'); % select the file(s) from gui
File = [f_path '\' f_name]; % getting the file path and name
S = load(File); % loading file to a structure
fn = fieldnames(S); % get the variable names in the file.
Fc = length(fn); % num of variables in the structure file
for k = 1:1:Fc; % running look to process data
F = fn{k}; % field variable name
F = strrep(F, 'YOUR TEXT GOES HERE', ''); % replacing undesireable text from variable name
if dwn_smpl > 1
S.([fn{k} str_var]) = downsample(S.(F),dwn_smpl); % downsampling data
else
S.([fn{k} str_var]) = upsample(S.(F),dwn_smpl); % up sampling data
end
end
%% saving file based off up sampling or down sampling
if dwn_smpl > 1
save([f_name(1:end-4) '_down_sampled.mat'], '-struct', 'S') % extracting data from structure and saving as .mat file
else
save([f_name(1:end-4) '_up_sampled.mat'], '-struct', 'S') % extracting data from structure and saving as .mat file
end
disp (['All variables down sampled to: ' num2str(desired_freq) 'Hz']);
toc

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Produkte


Version

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by