Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how can expand a m file?

Asked by Mahan Soltani on 17 Dec 2012

this is a m file for one xml file. how can i change it for show(plot,mat file and etc)more exel files? m file is :

clc
clear all
close all
[numdata,txtdata]=xlsread(['subject_1.xls']);
Ankle_saj_1=numdata(:,2);
max_pf_1=min(Ankle_saj_1);
max_df_1=max(Ankle_saj_1);
save('Ankle_saj_var_1','max_pf_1','max_df_1')
plot(Ankle_saj_1)
saveas(gcf,'Ankle_saj_1.fig')

[Merged information from duplicate question]

i have this m file:

[numdata,txtdata]=xlsread(['subject_1.xls']);
Ankle_saj_1=numdata(:,2);
max_pf_1=min(Ankle_saj_1);
max_df_1=max(Ankle_saj_1);
save('Ankle_saj_var_1','max_pf_1','max_df_1')
plot(Ankle_saj_1)
saveas(gcf,'Ankle_saj_1.fig')

-this m file collect data (Ankle_saj_1 , max_pf_1 , max_df_1 and plot(Ankle_saj_1)) from subject_1.xls and show them and finally save it. now, i have 10 xls file (subject_1 subject_2 ,...,subject_10). i need this data for all xls file to compare them.(for example: show 10 plot in one window to see all). Joes said that i need structure and loop to show all of them.i'm amautor and dont know about this works. please help me,this is for CP persons.thanks. download m file

3 Comments

Mahan Soltani on 17 Dec 2012

no, same problem

Jan Simon on 17 Dec 2012

@Mohammad: If this is the same problem, please do not post another question. Such "double posting" is inefficient for you and for all readers, because it is additional work to keep the overview over both threads. Therefore double-posts are delete usually.

Mahan Soltani

Tags

Products

2 Answers

Answer by Walter Roberson on 17 Dec 2012
Edited by Walter Roberson on 17 Dec 2012
Accepted answer

The code could be simplified a fair bit if it was not necessary to save the information to individual .mat files using different variable names for each .mat file, and also if it was not necessary to save the individual plots.

for K = 1 : 10
    xlsfile = sprintf('subject_%d.xlsx', K);
    matfile = sprintf('results_%d.mat', K);
    saj_field = sprintf('Ankle_saj_%d', K);
    pf_field = sprintf('max_pf_%d', K);
    df_field = sprintf('max_df_%d', K);
    plotfile = [saj_field '.fig'];
    [numdata,txtdata] = xlsread(xlsfile);
    all_saj{K} = numdata(:,2);
    yourStruct.(saj_field) = all_saj{K};
    yourStruct.(pf_field) = min( all_saj{K} );
    yourStruct.(df_field) = max( all_saj{K} );
    save(matfile, '-struct', 'yourStruct');
    plot(all_saj{K});
    saveas(gcf, potfile);
end
ph(1) = plot(all_saj{1});
hold all
for K = 2 : length(all_saj)
   ph(2) = plot(all_saj{K});
end
legend(ph, cellstr( num2str( (1:length(all_saj)).', 'Subject #%d' )));

7 Comments

Walter Roberson on 17 Dec 2012

Re-corrected.

You really should be able to handle these minor things (such as reversing two arguments) by yourself. Learn to read the documentation.

http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab

Walter Roberson on 18 Dec 2012
for K = 1 : 10
    xlsfile = sprintf('subject_%d.xlsx', K);
    matfile = sprintf('results_%d.mat', K);
    saj_field = sprintf('Ankle_saj_%d', K);
    pf_field = sprintf('max_pf_%d', K);
    df_field = sprintf('max_df_%d', K);
    plotfile = [saj_field '.fig'];
    [numdata,txtdata] = xlsread(xlsfile);
    all_saj{K} = numdata(:,2);
    yourStruct.(saj_field) = all_saj{K};
    yourStruct.(pf_field) = min( all_saj{K} );
    yourStruct.(df_field) = max( all_saj{K} );
    save(matfile, '-struct', 'yourStruct');
    plot(all_saj{K});
    saveas(gcf, plotfile);
end
ph(1) = plot(all_saj{1});
hold all
for K = 2 : length(all_saj)
   ph(2) = plot(all_saj{K});
end
legend(ph, cellstr( num2str( (1:length(all_saj)).', 'Subject #%d' )));
Walter Roberson on 18 Dec 2012

Change "potfile" to "plotfile" yourself. And comment out the call to legend() if it is giving you problems.

Walter Roberson
Answer by Walter Roberson on 17 Dec 2012

In order to use it for more excel files, you need to remove the "clear all"

4 Comments

Walter Roberson on 17 Dec 2012

I assure you, you have no chance of getting that program to work for multiple files unless you remove the "clear all".

Walter Roberson on 17 Dec 2012

No. See http://www.mathworks.com/help/matlab/ref/clear.html

* Clear All Variables And Functions
    clear all

This is not for erasing the previous command. "clear all" is like driving a car down a highway and suddenly all of the gasoline is gone and suddenly all of the air is gone from the tires and suddenly the car is turned off and the keys are not even in the ignition anymore.

Walter Roberson on 17 Dec 2012

Perhaps you were composing that comment while I was posting the answer...

The problem is not simple because you want the saved variable names to match the subject number.

Walter Roberson

Contact us