1
votes

I have a function in matlab.

function [MEAN STD]=result(data)
MEAN=[mean(data)];
STD=std(data);
Training_data=[MEAN STD]
savefilename=sprintf('%s',inputname(1))
save(savefilename,'Training_data')
end

i set data's filename=ET1_A_l1(imported to workspace alrdy)(ET1_A_l1=[1;2;1;3;1;4] to find the mean and std of selected data(ET1_A_l1),and save the statistical feature into .mat form as shown in below:

>>[MEAN STD]=result(ET1_A_l1)

As a result, the name of save file is ET1_A_l1.mat,and I import ET1_A_l1.mat into the workspace,it shows the 'Training_data' as shown in figure 1..

figure 1 ,

is there any good idea to change the variable name(Training_data) to ET1_A_l1 in workspace??

1

1 Answers

0
votes

To change the variable name after loading (not the field name), try this:

clear all; close all;

fname = 'ET1_A_l1';
Training_data=[0.5 .1]; % test data

savefilename=sprintf('%s', [fname '.mat']);
save(savefilename,'Training_data');

clear Training_data;

% important line
eval([fname '=importdata(''' savefilename ''');']);

EDIT To change the field names directly:

function [MEAN STD]=result(data)
    MEAN = mean(data);
    STD = std(data);
    varname = inputname(1);
    eval([varname '=[MEAN STD];']);
    savefilename = sprintf('%s',varname);
    save(savefilename, varname);
end

>> ET1_A_l1=[1;2;1;3;1;4];
>> >> [m s] = result(ET1_A_l1)

m =
     2
s =
    1.2649

>> clear all;
>> in = load('ET1_A_l1');

>> fieldnames(in)
ans = 
    'ET1_A_l1'

enter image description here